Problem in SharePoint Object model when accessing the sharepoint list items?
just i wrote
using (SPSite site = SPContext.Current.Site) { using (SPWeb web = site.OpenWeb()) { //SPList lst = web.Lists["ManagerInfo"]; SPList lst = web.Lists[strlist];
SPQuery getUserNameQuery = new SPQuery();
// getUserNameQuery.Query = "<Where><And><Eq><FieldRef Name=\"Region\" /><Value Type=\"Text\">" + strRegion + "</Value></Eq><And><Eq><FieldRef Name=\"PM_x0020_First_x0020_Name\" /><Value Type=\"Text\">" + pmFName + "</Value></Eq><Eq><FieldRef Name=\"PM_x0020_Last_x0020_Name\" /><Value Type=\"Text\">" + pmLname + "</Value></Eq></And></And></Where>";
// getUserNameQuery.Query = "<Where><And><Eq><FieldRef Name=\"PM_x0020_First_x0020_Name\" /><Value Type=\"Text\">" + pmFName + "</Value></Eq><Eq><FieldRef Name=\"PM_x0020_Last_x0020_Name\" /><Value Type=\"Text\">" + pmLname + "</Value></Eq></And></Where>";
getUserNameQuery.Query = "<Where><Eq><FieldRef Name=\"PM_x0020_Name\" /><Value Type=\"Text\">" + loginName + "</Value></Eq></Where>";
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);
}
} }
i used this code in infopath this infopath is using by all users.os when the current login user have no permissions to the list it showing error.when the current login user have full Permission it is working. So Please advise me what can i do inorder to work them for all users.
By using this
using (SPSite site = SPContext.Current.Site){}
You are disposing of SPContext.Current.Site which you should not do. Taking Don's suggestion into consideration as well, try this
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 = "<Where><And><Eq><FieldRef Name=\"Region\" /><Value Type=\"Text\">" + strRegion + "</Value></Eq><And><Eq><FieldRef Name=\"PM_x0020_First_x0020_Name\" /><Value Type=\"Text\">" + pmFName + "</Value></Eq><Eq><FieldRef Name=\"PM_x0020_Last_x0020_Name\" /><Value Type=\"Text\">" + pmLname + "</Value></Eq></And></And></Where>";
// getUserNameQuery.Query = "<Where><And><Eq><FieldRef Name=\"PM_x0020_First_x0020_Name\" /><Value Type=\"Text\">" + pmFName + "</Value></Eq><Eq><FieldRef Name=\"PM_x0020_Last_x0020_Name\" /><Value Type=\"Text\">" + pmLname + "</Value></Eq></And></Where>";
getUserNameQuery.Query = "<Where><Eq><FieldRef Name=\"PM_x0020_Name\" /><Value Type=\"Text\">" + loginName + "</Value></Eq></Where>";
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);
}
}
}
});
try to surround your code logic with :
SPSecurity.RunWithElevatedPrivileges(delegate()
{
// insert code here
}
精彩评论