locked
User Profile Photo upload RRS feed

  • Question

  • Hello All - I am trying to upload the picture of the selected user using webpart following http://nishantrana.me/2013/12/18/upload-user-profile-picture-programmatically-in-sharepoint-2013/ article. I am executing with elevated privilege (Farm Admin) still it is throwing error that "Site has not been shared with you" on

    SPFolder subfolderForPictures = web.Folders["User Photos"].SubFolders["Profile Pictures"];

    I checked that Admin has permission on "Profile Pictures" folder and "User Photos" library as well as on mysite.

    Could anybody please guide me? 

    Regards,


    Khushi


    Friday, December 18, 2015 8:19 PM

Answers

  • Thank you David for your response. I don't know for whatever reason I was not able to do it. I gave up because I spent too much time on it and fall behind other tasks in my plate. So, I found other way to achieve it by setting up delegate rights as below and solved my problem.

    Reference : http://sharepoint.stackexchange.com/questions/3328/sharepoint-2010-allowing-users-to-modify-other-users-profile-properties

    Many Many thanks to Vassili Altynikov

     Thanks Everyone!!

    Regards,


    Khushi


    • Edited by KhushiSShaikh Tuesday, December 22, 2015 10:28 PM
    • Proposed as answer by Victoria Xia Wednesday, December 23, 2015 12:46 AM
    • Marked as answer by Victoria Xia Friday, December 25, 2015 9:07 AM
    Tuesday, December 22, 2015 10:26 PM

All replies

  • Hi Khushi,

    Can you post your entire method or at least the entire portion that you are doing the upload with.  A common mistake with elevated privileges is executing the code inside the delegate, but using objects that were declared outside of the delegate, which causes the error you are getting.

    Example:

    Using(SPSite site as new SPSite("Your site")
    {
      Using(SPWeb web = site.rootweb)
      {
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
          ...
          //Using a web object created outside the elevated privledges
          SPFolder subfolderForPictures = web.Folders["User Photos"].SubFolders["Profile Pictures"];
        });
      }
    }

    Proper way would be:

    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
     Using(SPSite site as new SPSite("Your site")
     {
      Using(SPWeb web = site.rootweb)
      {
          ...      
          SPFolder subfolderForPictures = web.Folders["User Photos"].SubFolders["Profile Pictures"];    
      }
     }
    });



    • Edited by David Drever Friday, December 18, 2015 8:41 PM New signature... looked awful.
    Friday, December 18, 2015 8:39 PM
  • Hello David - I am doing it the second way. 

    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        using (SPSite mySite = new SPSite(userProfileMgr.MySiteHostUrl))
                        {
                            using (SPWeb web = mySite.RootWeb)
                            {
                                SPFolder subfolderForPictures = web.Folders["User Photos"].SubFolders["Profile Pictures"];

                                web.AllowUnsafeUpdates = true;

    ...

                            }

                        }

    });

    It's throwing error at SPWeb web = mySite.RootWeb line.

    Thank you for your reply.  


    Khushi

    Friday, December 18, 2015 9:39 PM
  • Think I forgot something.  I try getting the SPSite object inside the elevation using the ID like this:

    Using(SPSite spSite = new SPSite(userProfileMgr.MySiteHostUrl))
    {
      SPSecurity.RunWithElevatedPrivileges(delegate()
      {
       Using(SPSite site = new SPSite(spSite.ID)
       {
        Using(SPWeb web = site.rootweb)
        {
            ...      
            SPFolder subfolderForPictures = web.Folders["User Photos"].SubFolders["Profile Pictures"];    
        }
       }
      });
    }

    Grab the new site object inside the delegate using the ID of the one created outside of the delegate.


    Best Regards,

    David Drever BSc, MCST

    Senior SharePoint Conultant

    Blog: http://www.prairiedeveloper.com  Twitter:   LinkedIn:

    Note: Posts are provided “AS IS” without warranty of any kind, either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

    Friday, December 18, 2015 10:08 PM
  • Hi Khushi,

    The way to use SPSecurity.RunWithElevatedPrivileges is correct, but did current user have permission on the current site where the code ran?

    Please make sure that current user has read permission at least on the current site where the code runs.

    For narrowing down the issue, please use system account to see if the code can be executed correctly.

    Best regards,

    Victoria

    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    Monday, December 21, 2015 8:18 AM
  • Thanks Victoria I tried with the System Account too and it is not working. I then checked permission of the account which is site collection administrator for mysite on User Photos/Profile Pictures and it is showing as below

    Please help I have spent 2 days to resolve this issue and i am not able to update a user profile picture from a webpart. 

    Regards


    Khushi

    Monday, December 21, 2015 8:46 PM
  • Khushi,

    Are you sure your code is throwing the exception at the creation of the SPWeb object.  It should be working.  

    When you checked the access, did you check it on the MySite?  I have written a similar program in VB.NET (don't ask it was a dark time) and while I also had to run with elevated privileges, it was pretty straight forward.

    Within elevated privileges, I grabbed the site using the URL (as you have already done).  This time grab the list and cast it as type of document library.  The folder is accessed via the list and not the site (which is why I am asking if you are sure the error is coming from the site declaration and not the creation of your subfolderForPictures object.  So instead of doing it the way you are, can you try something like this:

    SPDocumentLibrary spLibrary = (SPDocumentLibrary)spWeb.Lists["<List Name>"] as ;
    SPFolder spRootFolder = spList.RootFolder;
    
    string folderName = <Name of Folder> + "\";
    SPFile newFile = spRootFolder.Files.Add(folderName + fileName, <binary data form of file>, true);
    spLibrary.Update();

    Does that help you along any?


    Best Regards,

    David Drever BSc, MCST

    Senior SharePoint Conultant

    Blog: http://www.prairiedeveloper.com  Twitter:   LinkedIn:

    Note: Posts are provided “AS IS” without warranty of any kind, either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.


    • Edited by David Drever Monday, December 21, 2015 9:38 PM Minor error spotted in code
    Monday, December 21, 2015 9:28 PM
  • Thank you David for your response. I don't know for whatever reason I was not able to do it. I gave up because I spent too much time on it and fall behind other tasks in my plate. So, I found other way to achieve it by setting up delegate rights as below and solved my problem.

    Reference : http://sharepoint.stackexchange.com/questions/3328/sharepoint-2010-allowing-users-to-modify-other-users-profile-properties

    Many Many thanks to Vassili Altynikov

     Thanks Everyone!!

    Regards,


    Khushi


    • Edited by KhushiSShaikh Tuesday, December 22, 2015 10:28 PM
    • Proposed as answer by Victoria Xia Wednesday, December 23, 2015 12:46 AM
    • Marked as answer by Victoria Xia Friday, December 25, 2015 9:07 AM
    Tuesday, December 22, 2015 10:26 PM
  • So this tells me that for whatever reason, your code is not running with full privileges, otherwise you wouldn't need to give it directly to the user.  Not sure why as it should be working fine.

    Thanks,

    Dave


    Best Regards,

    David Drever BSc, MCST

    Senior SharePoint Conultant

    Blog: http://www.prairiedeveloper.com  Twitter:   LinkedIn:

    Note: Posts are provided “AS IS” without warranty of any kind, either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

    Tuesday, December 22, 2015 10:30 PM
  • Yes David. Unfortunately that's what it is. I really did not want to give normal user to manage profiles and that was the reason I was creating custom page for this user but i had no choice. 

    Thanks for your responses. 

    Regards,


    Khushi

    Wednesday, December 23, 2015 8:00 PM