Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

is there a way to pass a parameter to the GetSubwebsForCurrentUser() function? I'm needing to get the subwebs for specific users - not the current user. Any help would be appreciated. Thanks,

share|improve this question

2 Answers 2

up vote 1 down vote accepted

You can impersonate a particular user, but obviously this involves re-creation of SPSite and SPWeb objects. Thus please use this approach carefully: calling the code from a loop can cause unwanted performance leak.

//get this current user's user token
SPUserToken userToken = web.AllUsers[user].UserToken;

//create an SPSite object in the context of this user
using (SPSite s = new SPSite(siteStr, userToken))
{

    using (SPWeb w = s.OpenWeb())
    {
       // use w.GetSubwebsForCurrentUser() here
    }
}

More details and explanations here: http://blackninjasoftware.com/2009/04/09/how-to-programmatically-impersonate-users-in-sharepoint/

share|improve this answer
    
Dangit... you guys beat me. Also, Keith Dahlby has a pretty interesting post about doing this for the system account. –  Kit Menke Jan 24 '12 at 22:57
    
Thanks, looks like we'll need to write a good method for this... –  ironman99 Jan 25 '12 at 14:34

You can impersonate other users when opening an SPSite (and thus a web below it)

SPUserToken userToken = web.AllUsers[user].UserToken;
using (SPSite site = new SPSite(siteUrl, userToken))
{
   using (SPWeb web = site.OpenWeb())
   {
    // ...
   }
}

But I would not guarantee the performance if you are looping through many users..

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.