Steps
  • Open Visual Studio in your system
  • Select Console Applciation template and give as name .
  • Add a Microsoft.Cleint Assembly refrence file in right side refrence tab in visual studio.
  • Replace Program.cs with the source code
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.SharePoint.Client;
  7. using Microsoft.SharePoint.Client.Social;
  8. namespace EmbedDocumentInPost
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. // Replace the following placeholder values with the actual values.
  15. const string serverUrl = "http://gauti.sharepoint.com/sp";
  16. const string documentUrl = "http://gauti.sharepoint.com//Shared%20Documents/fileName.docx";
  17. try
  18. {
  19. // Get the context and the SocialFeedManager instance.
  20. ClientContext clientContext = new ClientContext(serverUrl);
  21. SocialFeedManager feedManager = new SocialFeedManager(clientContext);
  22. // Get the document attachment from the server.
  23. ClientResult<SocialAttachment> attachment = feedManager.GetPreview(documentUrl);
  24. clientContext.ExecuteQuery();
  25. // Define properties for the post and add the attachment.
  26. SocialPostCreationData postCreationData = new SocialPostCreationData();
  27. postCreationData.ContentText = "Post with a document.";
  28. postCreationData.Attachment = attachment.Value;
  29. // Publish the post. This is a root post to the user's feed, so specify
  30. // null for the targetId parameter.
  31. feedManager.CreatePost(null, postCreationData);
  32. clientContext.ExecuteQuery();
  33. Console.Write("The post was published.");
  34. Console.ReadLine();
  35. }
  36. catch (Exception ex)
  37. {
  38. Console.Write("Error publishing the post: " + ex.Message);
  39. Console.ReadLine();
  40. }
  41. }
  42. }
  43. }