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.

References:
http://www.sharepointdeveloperhq.com/2009/04/how-to-programmatically-impersonate-users-in-sharepoint/


No comments:

Post a Comment