Saturday, July 26, 2014

How to programmatically impersonate users in SharePoint 2010

Hi All,

Here I am Creating SPUserToken of a SharePoint user. Purpose is to get filter list data of where he/she is a member of "OwnerGroup". Where "OwnerGroup" is a column in List.  

SPSite tempSite = new SPSite("http://siteUrl");
SPUserToken userToken = tempSite.OpenWeb().EnsureUser("i:0#.f|ldapmembership|itmohit").UserToken;

                using (SPWeb web = new SPSite("http://siteUrl", userToken).OpenWeb())
                    SPList list = web.Lists["Customers"];
                    SPQuery query = new SPQuery();
                    query.Query = "<Where><Membership Type='CurrentUserGroups'><FieldRef Name='OwnerGroup'/></Membership></Where><OrderBy><FieldRef Name='ID' Ascending='FALSE' /></OrderBy>";

                    SPListItemCollection itemColl = list.GetItems(query);

                    if (itemColl.Count > 0)


That's it.


No comments:

Post a Comment

SharePoint Deployment Error : The specified path, file name, or both are too long.

Hi, Sometimes we get surprised with below error while deploying wsp file... Solution : To overcome with this issue, we just ne...