Embed an image in a post SharePoint Server 2013 using CSOM

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 EmbedImageInPost  
  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 imageUrl = "http://gauti.sharepoint .com/Shared%20Documents/testimage.jpg";  
  17.         // Define the image attachment that you want to embed in the post.  
  18.         SocialAttachment attachment = new SocialAttachment()  
  19.          {  
  20.             AttachmentKind = SocialAttachmentKind.Image,  
  21.             Uri = imageUrl  
  22.          };  
  23.   
  24.       // Define properties for the post and add the attachment.  
  25.       SocialPostCreationData postCreationData = new SocialPostCreationData();  
  26.       postCreationData.ContentText = "Look at this!";  
  27.       postCreationData.Attachment = attachment;  
  28.   
  29.    try  
  30.    {  
  31.       // Get the context and the SocialFeedManager instance.  
  32.       ClientContext clientContext = new ClientContext(serverUrl);  
  33.       SocialFeedManager feedManager = new SocialFeedManager(clientContext);  
  34.       // Publish the post. This is a root post to the user's feed, so specify  
  35.       // null for the targetId parameter.  
  36.       feedManager.CreatePost(null, postCreationData);  
  37.       clientContext.ExecuteQuery();  
  38.       Console.Write("The post was published.");  
  39.       Console.ReadLine();  
  40.    }  
  41.    catch (Exception ex)  
  42.       {  
  43.       Console.Write("Error publishing the post: " + ex.Message);  
  44.       Console.ReadLine();  
  45.       }  
  46.    }  
  47.  }  
  48. }  
Hit F5 and check the output .
 
Thanks for learning in my Blogs!!!!
Ebook Download
View all
Learn
View all