Sharepoint 2013 Social API How to do customization in newsfeed

Sharepoint 2013 Social API
How to do customization in newsfeed




1.How to mention some one in newsfeed using Server Object Model


This code can be written on a button click.
I am fetching value from text box and posting it to newsfeed.
This codes works for different webapplication in same farm.Which means you can post from Team site to mysite etc,


            SPWeb currentWeb = SPContext.Current.Web;
            SPUser currentUser = SPContext.Current.Web.CurrentUser;
     
            SPUtility.ValidateFormDigest();
//here you need to provide sam account name
            string accountName = @"domain\psno";
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                //Get the UserProfil for target user
                SPUser user = currentWeb.EnsureUser(accountName);
                SPServiceContext serverContext = SPServiceContext.GetContext(currentWeb.Site);
                UserProfileManager profileManager = new UserProfileManager(serverContext);
                UserProfile profile = profileManager.GetUserProfile(currentUser.LoginName);
           
                SPUserToken userToken = currentUser.UserToken;
                SPSocialDataItem userMentionLink = new SPSocialDataItem
                {
                    ItemType = SPSocialDataItemType.User,
                    AccountName = user.ToString()

                };
                SPSocialPostCreationData postCreationData = new SPSocialPostCreationData();
                postCreationData.ContentText = "{0}"+" "+TextBox1.Text;
                postCreationData.ContentItems = new SPSocialDataItem[1] { userMentionLink };
                if (profile != null)
                {
                 

                    //SPSocialDataItem socialDataMentionItem = new SPSocialDataItem();
                    //socialDataMentionItem.ItemType = SPSocialDataItemType.User;
                    //socialDataMentionItem.AccountName = "domain\\psno";





                    SPSocialFeedManager _f = new SPSocialFeedManager(profile, serverContext, userToken);
               
                    _f.CreatePost(null, postCreationData);
                    TextBox1.Visible = false;
                    Button3.Visible = false;
                    Label1.Visible = true;
                    Label1.Text = "You wish has been posted succeessfully !!!";

   

     
Output of the above code:

Comments