Can not retrieve 'Documents' of my site with the user token of site administrator RRS feed

  • Question

  • Hi Team,

    Recently, we found an issue that we can not retrieve the 'Documents' of My site if we init the SPSite object under the user token of the my site adminstrator. What we want to do is to upload documents to the 'Documents' by specified user

    attached the test code snippet

     using (SPSite site = new SPSite(siteUrl))
                    var spuser = site.RootWeb.EnsureUser(userName);
                    var token = spuser.UserToken;
                    //token = site.SystemAccount.UserToken;
                    using (SPSite tokenSite = new SPSite(siteUrl, token))
                         var splist = tokenSite.RootWeb.Lists["Documents"];


    if use the user token of system account, it worked fine. 

    The user have full control of the Documents and can manipulate the library on UI

    I found that the Documents' base template is mysitedocumentlibrary and related to the onedrive function, any relationship with this issue?

    Could anyone help to explain it?

    Thanks a lot!


    Tuesday, March 7, 2017 8:36 AM

All replies

  • Hi Louis,

    Please check the parameter in Web.Ensure method is valid, it will be the format like below:

    var spuser = site.RootWeb.EnsureUser(@"contoso\jerry");

    I tested the code below and it will work:

                    SPSite site = new SPSite("http://sp/sites/teams");
                    var spuser = site.RootWeb.EnsureUser(@"contoso\jerry");
                    var token = spuser.UserToken;
                    using (SPSite tokenSite = new SPSite("http://sp/sites/teams", token))
                         var splist = tokenSite.RootWeb.Lists["Documents"];


    Best Regards

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact

    Wednesday, March 8, 2017 6:05 AM