3

I'm quite desperate, I working on project when I'd like to impersotane users and create post on behalf of them, read theirs feeds and according of those do some actions. I have figured out how to post on another user newsfeed, but still cannot read all. I have tried function GetFeedFor(), but this give you back just threads, no likes, no follows. I have been trying impersonate the process accordingly:

using (SPSite site = new SPSite(myHostUrl))
            {
                SPUser userForContext = site.RootWeb.SiteUsers[account2];
                SPUserToken userToken = userForContext.UserToken;
                //create service context
                SPServiceContext serviceContext = SPServiceContext.GetContext(site);
                //using Service Context Scope
                using (new SPServiceContextScope(serviceContext))
                {
                    //create profile manager for loading my test accounts
                    UserProfileManager profileManager = new UserProfileManager(serviceContext);
                    UserProfile userProfile = profileManager.GetUserProfile(account2);
                    /*List all NewsFeeds*/
                    SPSocialFeedManager feedManager = new SPSocialFeedManager(userProfile, serviceContext, userToken);
                    //here is exception thrown away
                    SPSocialFeed feed =  feedManager.GetFeed(SPSocialFeedType.Personal, new SPSocialFeedOptions());
                    //this write post on user's newsfeed and works!
                    WritePost(feedManager, "Forth Test!");
                }
            }

Whole stack is here:

Microsoft.Office.Server.Social.SPSocialException was unhandled
  HResult=6
  Message=The operation failed because an internal error occurred. Internal type name: Microsoft.Office.Server.Microfeed.MicrofeedException. Internal error code: 58.
  Source=Microsoft.Office.Server.UserProfiles
  ErrorCode=6
  InternalErrorCode=58
  InternalMessage=ServerErrorSecurityTrimmer : No Security Trimmer found  : Correlation ID:00000000-0000-0000-0000-000000000000 : Date and Time : 13/09/2013 16:44:27
  InternalStackTrace=   at Microsoft.Office.Server.Microfeed.MicroBlogEntity.SecurityTrimMicroFeed(List`1 mbThreads, SPMicrofeedRetrievalOptions retrievalOptions)
   at Microsoft.Office.Server.Microfeed.SPMicrofeedThreadCollection.SecurityTrimMicroFeed(SPMicrofeedRetrievalOptions retrievalOptions)
   at Microsoft.Office.Server.Microfeed.SPMicrofeedThreadCollection.Populate(SPMicrofeedRetrievalOptions retrievalOptions, SPMicrofeedContext context)
   at Microsoft.Office.Server.Microfeed.SPMicrofeedManager.CommonGetFeedFor(SPMicrofeedRetrievalOptions retrievalOptions)
   at Microsoft.Office.Server.Microfeed.SPMicrofeedManager.CommonPubFeedGetter(SPMicrofeedRetrievalOptions feedOptions, MicrofeedPublishedFeedType feedType, Boolean publicView)
   at Microsoft.Office.Server.Microfeed.SPMicrofeedManager.GetMyPublishedFeed(SPMicrofeedRetrievalOptions feedOptions, MicrofeedPublishedFeedType typeOfPubFeed, Boolean ShowPublicView)
   at Microsoft.Office.Server.Social.SPSocialFeedManager.Microsoft.Office.Server.Social.ISocialFeedManagerProxy.ProxyGetFeed(SPSocialFeedType type, SPSocialFeedOptions options)
   at Microsoft.Office.Server.Social.SPSocialFeedManager.c__DisplayClass2b.b__2a(ISocialFeedManagerProxy proxy)
   at Microsoft.Office.Server.Social.SPSocialFeedManager.c__DisplayClass4b`1.b__4a()
   at Microsoft.Office.Server.Social.SPSocialUtil.InvokeWithExceptionTranslation[T](ISocialOperationManager target, String name, Func`1 func)
  InternalTypeName=Microsoft.Office.Server.Microfeed.MicrofeedException
  StackTrace:
       at Microsoft.Office.Server.Social.SPSocialUtil.TryTranslateExceptionAndThrow(Exception exception)
       at Microsoft.Office.Server.Social.SPSocialUtil.InvokeWithExceptionTranslation[T](ISocialOperationManager target, String name, Func`1 func)
       at Microsoft.Office.Server.Social.SPSocialFeedManager.S2SInvoke[T](String name, SPS2SAppExecutionContextBase context, Func`2 func, Boolean elevateForIsLocalCheck)
       at Microsoft.Office.Server.Social.SPSocialFeedManager.c__DisplayClass48`1.b__47()
       at Microsoft.Office.Server.Social.SPSocialUtil.InvokeWithExceptionTranslation[T](ISocialOperationManager target, String name, Func`1 func)
       at Microsoft.Office.Server.Social.SPSocialFeedManager.S2SInvoke[T](String name, MicroBlogEntity mbeTarget, Func`2 func, Boolean elevateForIsLocalCheck)
       at Microsoft.Office.Server.Social.SPSocialFeedManager.GetFeed(SPSocialFeedType type, SPSocialFeedOptions options)
       at SocialTest.CreatePost.Program.Main(String[] args) in c:\Users\m.bodocky\Documents\Visual Studio 2012\Projects\SocialTest\SocialTest.CreatePost\Program.cs:line 54
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

Thank you for any advice!

2
  • Hello, I'd like to ask MS guys to comment functions, especially for using user contexts. The failure of my program is in using the constructor: SPSocialFeedManager feedManager = new SPSocialFeedManager(userProfile, serviceContext, userToken); => userProfile - is user profile of user who's running this application; serviceContext - is from your my site collection; userToken - is user token of user which is object of your query! Sep 16, 2013 at 7:59
  • Hello,<br/>this code does not work for me. It does not throw an exception but I always get the feed of the current user, no matter what UserToken I'm sending. I can test this behavior by running the code in a console app as different Windows users. I always get the feed of the user account the console is started with. > Might this be a bug in SharePoint's social API? Best regards<br/> Holger
    – user21829
    Jan 8, 2014 at 21:25

1 Answer 1

1

This is working solution:

using (SPSite site = new SPSite(myHostUrl))
            {
                //account1 is not current user
                SPUser userForContext = site.RootWeb.SiteUsers[account1];
                SPUserToken userToken = userForContext.UserToken;
                //create service context
                SPServiceContext serviceContext = SPServiceContext.GetContext(site);
                //using Service Context Scope
                using (new SPServiceContextScope(serviceContext))
                {
                    //create profile manager for loading my test accounts
                    UserProfileManager profileManager = new UserProfileManager(serviceContext);
                    // I'm getting user profile of my current user
                    UserProfile userProfile = profileManager.GetUserProfile(System.Environment.UserName);
                    SPSocialFeedManager feedManager = new SPSocialFeedManager(userProfile, serviceContext, userToken);
                    //It does work nicely!
                    SPSocialFeed feed =  feedManager.GetFeed(SPSocialFeedType.Personal, new SPSocialFeedOptions());
                }
            }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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