一千萬個為什麽

搜索

訪問共享點列表項時SharePoint對象模型中的問題?

我寫的

使用(SPSite site = SPContext.Current.Site)                   {                       使用(SPWeb web = site.OpenWeb())                       {                           // SPList lst = web.Lists [“ManagerInfo”];                           SPList lst = web.Lists [strlist];

                      SPQuery getUserNameQuery = new SPQuery();
                     //getUserNameQuery.Query = "" + strRegion + "" + pmFName + "" + pmLname + "";
                      //getUserNameQuery.Query = "" + pmFName + "" + pmLname + "";

                       getUserNameQuery.Query = "" + loginName  + "";

                      SPListItemCollection items = lst.GetItems(getUserNameQuery);
                      foreach (SPListItem item in items)
                      {
                          managerFName = item["Manager Name"].ToString();
                          strAccounting = item["Accounting"].ToString();

                          managerFName = managerFName.Replace(".", " ");
                          strAccounting = strAccounting.Replace(".", " ");
                         //isFound = true;
                          XPathNavigator managerName = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:txtManagerName", NamespaceManager);
                          managerName.SetValue(managerFName);
                          XPathNavigator accountingName = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:txtAccountingName", NamespaceManager);
                          accountingName.SetValue(strAccounting);
                      }

} }

我在infopath中使用此代碼,這個infopath正在被所有users.os使用當當前登錄用戶對列表沒有權限時它顯示錯誤。當前登錄用戶具有完全權限時它正在工作。 所以請告訴我我能做些什麽才能為所有用戶使用它們。

最佳答案

通過使用這個

使用(SPSite site = SPContext.Current.Site){}

你正在處理你不應該做的SPContext.Current.Site。考慮到Don的建議,試試這個

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite site = new SPSite(SPContext.Current.Site.ID))
    {
        using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
        {

            //SPList lst = web.Lists["ManagerInfo"]; 
            SPList lst = web.Lists[strlist];

            SPQuery getUserNameQuery = new SPQuery();
           //getUserNameQuery.Query = "" + strRegion + "" + pmFName + "" + pmLname + ""; 
           //getUserNameQuery.Query = "" + pmFName + "" + pmLname + ""; 

            getUserNameQuery.Query = "" + loginName + "";

            SPListItemCollection items = lst.GetItems(getUserNameQuery);
            foreach (SPListItem item in items)
            {
                managerFName = item["Manager Name"].ToString();
                strAccounting = item["Accounting"].ToString();

                managerFName = managerFName.Replace(".", " ");
                strAccounting = strAccounting.Replace(".", " ");
               //isFound = true; 
                XPathNavigator managerName = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:txtManagerName", NamespaceManager);
                managerName.SetValue(managerFName);
                XPathNavigator accountingName = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:txtAccountingName", NamespaceManager);
                accountingName.SetValue(strAccounting);
            }
        }
    }
});

轉載註明原文: 訪問共享點列表項時SharePoint對象模型中的問題?