1

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

1

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();
  • did not work :( – Rohander Aug 5 '13 at 15:25
  • I get this error screen "An attempt is made to use an SPWeb object that is closed or removed and is no longer valid." – Rohander Aug 5 '13 at 15:26
  • getting the same errors before this one – Rohander Aug 5 '13 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.