Thursday, August 25, 2011

Allowing anonymous users access to SharePoint user's profile pictures

I was developing discussion groups using web parts where I needed to show the discussion posting authors' profile images with the posting detail.

First problem I faced was, how to get the profile image url of the user. I was able to find the details about the User Information List living in the RootSite of the site collection. I was able to get the related user info by getting the list item as below and get the "Picture" property value
and properly formatting it. 

userPictureUrl = SPContext.Current.Site.RootWeb.SiteUserInfoList.GetItemById(itemCreator.ID)["Picture"].ToString(); 
SPFieldUrlValue userProfileImage = new SPFieldUrlValue(userPictureUrl);
authorProfileImageUrl = (new Uri(userProfileImage.Url)).AbsolutePath;
 
But, then I faced the next issue of viewing user's profile image as an anonymous user. To achieve that I had to go through the below process of granting anonymous users access to List and libraries in the mys site site collection and granting "View Items" access to user profile pictures library.

STEP 1:
Go to http://<<MysiteSitecollection web url>>/_layouts/settings.aspx
Go to "Site Permissions" and click Anonymous Access from the ribbon
Select "Lists and libraries" and click "OK".



STEP 2:
Go to View All Site Content -> User Photos -> Settings (Picture Library Settings)
Go to "Permissions for this picture library"
Click "Stop Inheriting Permissions"
Click "Anonymous Access" from the ribbon
Check off "View Items" and click "OK"


After performing the above steps anonymous users were able to get access to profile pictures of other users.

Senthil S

Thursday, August 18, 2011

Security validation exception when updating list items

Recently, I faced an exception when trying to update an existing list item programatically. When trying to update the list item i.e., when trying to do SPListItem.Update(), I was getting the below exception message
           
              "Microsoft.SharePoint.SPException: The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again."

After some time, I found that I need to set the "AllowUnsafeUpdates" property of the parent web of the list item to true before doing an update.

               targetWeb.AllowUnsafeUpdates = true;
               listItem["Title"] = newTitle;
               listItem.Update();
               targetWeb.AllowUnsafeUpdates = false;

This should work if the current user has proper privileges, otherwise the above code needs to be ran under an impersonated context.

Probably, the same will happen when creating, deleting a list item too.  Happy coding ...


Senthil