In this article, I’ll explain on how to post to Office 365 SharePoint Social Feed using .NET Client Object Model (CSOM).
Step1 Create a Console application by name ‘PublishToSocialFeed’
Step2 Add references to the following assemblies
Microsoft.SharePoint.Client
Microsoft.SharePoint.ClientRuntime
Microsoft.SharePoint.Client.UserProfiles
Step3 Add references to the following assemblies
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Social;
Step4
Create a Public Function by name ‘PublishPost’ and add the following snippet of code.
public static void PublishPost()
{
Uri oUri = new Uri(“https://yoursite.sharepoint.com”);
Office365ClaimsHelper claimsHelper = new Office365ClaimsHelper(oUri, “youruserid@yoursite.onmicrosoft.com”, “yourpassword”);
using (ClientContext oClientContext = new ClientContext(oUri))
{
oClientContext.ExecutingWebRequest += claimsHelper.clientContext_ExecutingWebRequest;
SocialFeedManager oSocialFeedManager = new SocialFeedManager(oClientContext);
//Create an instance of Social Data Item object
SocialDataItem oSocialDataItem = new SocialDataItem();
oSocialDataItem.ItemType = SocialDataItemType.Link;
oSocialDataItem.Text = “Google”;
oSocialDataItem.Uri = “http://google.com”;
//Create an instance of Social Post Creation Data
SocialPostCreationData oSocialPostCreationData = new SocialPostCreationData();
oSocialPostCreationData.ContentText = “This is my first post”;
oSocialFeedManager.CreatePost(null, oSocialPostCreationData);
oClientContext.ExecuteQuery();
Console.WriteLine(“Published a news feed”);
}
}
To know more about ‘Office 365 Helper’ class and active authentication for Office 365, please refer to Wictor Wilen’s article.