0
votes

I have this in a webpart inserted into a page from Sharepoint Admin Site (http://sharepointserver:port) and I want to read some info from mysite:

SPUserToken objSPUserToken = SPContext.Current.Site.SystemAccount.UserToken;
//SPUserToken objSPUserToken = SPContext.Current.Web.CurrentUser.UserToken;
using (SPSite mainSite = new SPSite("http://sharepointserver/sites/mysite", objSPUserToken))
{
    SPWeb mainWebSite = mainSite.OpenWeb();
    SPListItemCollection listItemsP = mainWebSite.Lists["Pages"].Items;
}

When I run it from Visual Studio, asks for user/password and executes normally, but when I publish the wsp and run it from browser, I have the message: Sorry, this site hasn't been shared with you

I've tried with the CurrentUser (commented line) and it gaves me the same message even from Visual Studio.

I'm sure that the user has rights into mysite, Do you have any idea of what´s happening?

Thanks!

2

2 Answers

0
votes

Modify the code as below.

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    //New SPSite object.
    using (SPSite mainSite = new SPSite("http://sharepointserver/sites/mysite"))
    {
        using (SPWeb mainWebSite =  mainSite.OpenWeb())
        {
            SPListItemCollection listItemsP = mainWebSite.Lists["Pages"].Items;
        }
    }
});

Reference: SPSecurity.RunWithElevatedPrivileges method

0
votes

I finally got it, the reason behind the message "Sorry, this site hasn't been shared with you" is because sharepoint log into the site not as the current user but the pool's account, so the message indicates that the site has not been shared for the sp_applicationpool or sp_farm accounts. I solved adding thats accounts into mysite.

Thanks for your help!