Question

I have a method in my aspx.cs file that should make sure that current user follow the current site. When I check ULS I receive this:

FollowedContent.FollowItem:Exception: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. ---> System.Runtime.InteropServices.COMException: The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again.0x8102006d

Could not follow the url http://removedlink/

and

Microsoft.Office.Server.Social.SPSocialFollowingManager.Follow: Microsoft.Office.Server.UserProfiles.FollowedContentException: InternalError : Could not follow the item X at Microsoft.Office.Server.UserProfiles.FollowedContent.FollowItem(FollowedItem item, Boolean isInternal)

My code looks like this:

 protected void ToggleUserFollowSite(object sender, EventArgs e)
        {

                var spWeb = SPContext.Current.Web;
                var currentsite = SPContext.Current.Site;
                var currentUser = spWeb.CurrentUser.LoginName;
                SPSecurity.RunWithElevatedPrivileges(delegate
                    {
                        using (var site = new SPSite(spWeb.Site.ID))
                        {
                            using (var web = site.OpenWeb(spWeb.ID))
                            {


                                SPServiceContext context = SPServiceContext.GetContext(currentsite);                                                                      
                                var profile = new UserProfileManager(context);                                    
                                var user = profile.GetUserProfile(currentUser);                                   
                                var followingManager = new SPSocialFollowingManager(user, context);                                    
                                var actorInfo = new SPSocialActorInfo();
                                actorInfo.ActorType = SPSocialActorType.Site;
                                actorInfo.ContentUri = new Uri(web.Url);
                                followingManager.Follow(actorInfo);

                            }
                        }
                    });
            }

When I debug this this is the exception I get at .Follow() method:

{"The operation failed because an internal error occurred. Internal type name: Microsoft.Office.Server.UserProfiles.FollowedContentException. Internal error code: 11."}

Any kind of help is appreciated

Was it helpful?

Solution

First, you should only use newly created SPSite and SPWeb instances inside RunWithElevatedPrivileges delegation. Change the following line in your code

SPServiceContext context = SPServiceContext.GetContext(currentsite);

to this one:

SPServiceContext context = SPServiceContext.GetContext(site);

Another problem is security validation on the page, and you need to call ValidateFormDigest method before performing the elevation since your code performs the write operation. So add the following line before SPSecurity.RunWithElevatedPrivileges in your code:

SPUtility.ValidateFormDigest();
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top