Dec 28

Written by: Michael Washington
12/28/2014 8:37 AM  RssIcon

image

Posting to the SharePoint Newsfeed from an AngularJS application is simplified when you build your AngularJS (https://angularjs.org/) application in a Visual Studio LightSwitch Cloud Business App project.

Posting to the SharePoint Newsfeed is simple when using a normal LightSwitch Cloud Business App. That process is described at this link. However, when using AngularJS, a call using the SharePoint client object model (CSOM) will provide the functionality required.

The Application

image

The AngularJS application runs as a SharePoint Provider Hosted Application inside a Visual Studio LightSwitch Cloud Business App project. It allows “To Do” items to be viewed and added.

image

When adding a new item, or selecting an exiting one (by clicking on it), a edit form appears.

image

After a item has been added, we can return to the main SharePoint site…

image

… select our “About me” page…

image

… and see that a post has been added to our Newsfeed.

Clicking on the link will take us directly to the application.

Creating The Application

image

We start with the SharePoint AngularJS application created in the article: An End-to-End AngularJS SharePoint Module using LightSwitch OData as a Back-End.

image

We open the project up and add Tenant permission.

image

We also add User Profiles (Social).

image

We open the ToDo entity in the editor…

image

… and select the _Inserting method.

We use the following code for the method:

 

        partial void ToDoes_Inserting(ToDo entity)
        {          
            //Load the properties for the SharePoint web object. 
            var clientContext = 
                this.Application.SharePoint.GetHostWebClientContext();
            Microsoft.SharePoint.Client.Web web = clientContext.Web;
            // Get the current web 
            clientContext.Load(web);
            clientContext.ExecuteQuery();
            // Get the AngularLightSwitchSP URL
            string AngularLightSwitchSP_url = "";
            // Get the WebCollection
            Microsoft.SharePoint.Client.WebCollection webs = web.Webs;
            clientContext.Load<Microsoft.SharePoint.Client.WebCollection>(webs);
            clientContext.ExecuteQuery();
            // Loop thru each web to find AngularLightSwitchSP
            foreach (Microsoft.SharePoint.Client.Web objWeb in webs)
            {
                if (objWeb.Title.ToLower() == "angularlightswitchsp")
                {
                    // Get the URL
                    AngularLightSwitchSP_url = objWeb.Url;
                }
            }
            // Create a link to include in the post.
            SocialDataItem linkDataItem = new SocialDataItem();
            linkDataItem.ItemType = SocialDataItemType.Link;
            linkDataItem.Text = "link";
            linkDataItem.Uri = AngularLightSwitchSP_url;
            // Define properties for the post.
            SocialPostCreationData postCreationData = new SocialPostCreationData();
            postCreationData.ContentText = 
                entity.CreatedByInfo.FullName + " posted " + entity.TaskName + " {0}.";
            postCreationData.ContentItems = new SocialDataItem[1] { linkDataItem };
            // Get the SocialFeedManager instance.
            SocialFeedManager feedManager = new SocialFeedManager(clientContext);
            // Publish the post. This is a root post, so specify null for the
            // targetId parameter. 
            feedManager.CreatePost(null, postCreationData);
            clientContext.ExecuteQuery();
        }

 

Note: You will need to add the following using statements to the class file:

using Microsoft.LightSwitch.Security.Server;
using Microsoft.SharePoint.Client.Social;

Note: You will need to add a reference to:

Microsoft.SharePoint.Client.UserProfiles

Located at  ..Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\

Additional Functionality

See the following article for additional functionality added to this application:

Creating SharePoint Word Documents with AngularJS using LightSwitch

Links – Microsoft

Work with social feeds in SharePoint 2013

Posting on SharePoint 2013 newsfeed on behalf of someone else

Links – LightSwitch Help Website

Creating SharePoint Word Documents with AngularJS using LightSwitch

An End-to-End AngularJS SharePoint Module using LightSwitch OData as a Back-End

An End-To-End LightSwitch SharePoint Workflow Application

Advanced SharePoint Workflows Using HTTP Calls To LightSwitch

Deploy A LightSwitch Application To Office 365 / SharePoint Online

Exploring SharePoint 2013 Visual Studio Cloud Business Apps (LightSwitch)

Implementing Documents in a SharePoint 2013 Cloud Business App (LightSwitch)

Creating A SharePoint Online Testing Site

Creating A LightSwitch SharePoint 2013 Multi-Tenant Provider-Hosted Application

Download Code

The LightSwitch project is available at http://lightswitchhelpwebsite.com/Downloads.aspx

(you must have Visual Studio 2013 (or higher) installed and a SharePoint Developer site to run the code)

2 comment(s) so far...


Gravatar

Re: AngularJS Posting To SharePoint Newsfeed Using Visual Studio LightSwitch

I registers lightswitchhelpwebsite.com site and receiving mail with verification. But when click to verification link, response message is "Invalid verification code". I registered your site three times, received same error message. What can I do to register your site.

By cahit on   5/29/2015 7:39 AM
Gravatar

Re: AngularJS Posting To SharePoint Newsfeed Using Visual Studio LightSwitch

@cahit - Click respond on the email verification email and tell me it wont let you in and I will fix it.

By Michael Washington on   5/29/2015 8:42 AM

Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
CAPTCHA image
Enter the code shown above in the box below
Add Comment   Cancel 
Microsoft Visual Studio is a registered trademark of Microsoft Corporation / LightSwitch is a registered trademark of Microsoft Corporation