Friday, March 8, 2013

Push Newsfeed to Sharepoint 2013 Site From External Sources

SharePoint 2013 comes with a lot of new features especially for Social Networking. News Feed is one of those features. SharePoint 2010 also has that feature but External systems can't send external feeds to the SharePoint farm. SharePoint 2013 has defined some libraries (API) to push feeds from outside to SharePoint farm.
In addition to that there are some different between 2010 newsfeed and SharePoint 2013 Newsfeed.
 
Prerequisites

1. Configured SharePoint 2013 Farm  
2. MySiteHost URL or Team Site URL
3. Visual Studio 2012 installed in machine

Steps to create the Project to send Newsfeed to SharePoint My site or Team site


1. Create New Class library in Visual Studio 2012 and named it as "NewsFeedPushLib"


2. Create a file and named it as "NewsFeedPushClass.cs"

3. Add below dlls as reference to that project 


4. write the following code into that class.


a. Check for accessibility - Need to ensure that that user has enough rights to push feeds into share point site.
 
          System.Net.NetworkCredential cred = new System.Net.NetworkCredential("UserName", "Password");
          ClientContext clientContext = new ClientContext("siteURL");
          clientContext.Credentials = cred;


b. Get the thread owner from the PeopleManager object.

          Microsoft.SharePoint.Client.UserProfiles.PersonProperties owner = new Microsoft.SharePoint.Client.UserProfiles.PeopleManager(clientContext).GetPropertiesFor(
"UserName");
 

c. Get the MicrofeedManager object.

                MicrofeedManager microfeedManager = new MicrofeedManager(clientContext);


d. Register the requests that you want to run.
                // The first call requests only the DisplayName and AccountName
                // properties of the owner object.


                clientContext.Load(owner, o => o.DisplayName, o => o.AccountName);
                clientContext.Load(microfeedManager);


e. Run the requests on the server.
                clientContext.ExecuteQuery();


f. Define defenitionName

 MicrofeedPostOptions postOptions = new MicrofeedPostOptions();
postOptions.DefinitionName = "defenitionName";

Eg : "Microsoft.SharePoint.Microfeed.UserPost" for User Post  

g. Send the Feed

    if (!string.IsNullOrEmpty(feedItem))
     // remove the empty/Null feed                 

   {
         if (feedItem.Length > 512)
         {
               feedItem = feedItem.Substring(0, 508) + "...";
// to limit the feed limit
         }
         postOptions.Content = feedItem; 
// content for the feed 

         if ((!(feedItem == null)) && !(feedItem.Equals("")))
        {
            if (true)
            {
                postOptions.TargetActor = targetActor;
// set the target actor for a feed               
                microfeedManager.Post(postOptions); 
//post the feed on each user's newsfeed
                clientContext.ExecuteQuery();
                success = true;
            }
            else
            {
                success = false;
//couldn't push it to target no valid target
            }

        }
   }

}

5. Create another project (console Application) and create new file. then call Push newsfeed method in this class

 NewsFeedPushClass.PushToSharePoint("Testing Feed1", "Sharepoint_Web_application_URL", "domain\\user", "password", "Team Site URL", "Microsoft.SharePoint.Microfeed.UserPost");

6. Run the program and check whether it is working or not.



Sample feed(Defect Information) pushed from external system(Project management system) to Team Site

 
Download the Solution File - MediaFire Link