Blog of Sundar Narasiman

How to publish a post to SharePoint Social Feed using SharePoint 2013 JSOM

In this article, I’ll explain how to publish a Post to the SharePoint Social Feed using SharePoint 2013 JavaScript Object Model (JSOM).Open Visual Studio 2013, Create a new SharePoint Hosted App and name it as PublishPost.

Go to default.aspx, add a reference to SP.UserProfiles.js in the PlaceHolderAdditionalPageHead

<script type=”text/javascript” src=”/_layouts/15/sp.userprofiles.js”></script>

Add the following Javascript function in the App.JS file (comment out the existing code already in the file)

SP.SOD.executeOrDelayUntilScriptLoaded(WritePost, ‘SP.UserProfiles.js’);

function WritePost() {
    var oclientContext;
    var ofeedManager;
    var oresultThread;

    // Initialize the current client context and the SocialFeedManager instance.
    oclientContext = SP.ClientContext.get_current();
    ofeedManager = new SP.Social.SocialFeedManager(oclientContext);

    // Add a link to be included in the post.
    var olinkDataItem = new SP.Social.SocialDataItem();
    olinkDataItem.set_itemType(SP.Social.SocialDataItemType.link);
    olinkDataItem.set_text(‘My blog url’);
    olinkDataItem.set_uri(‘
http://sundarnarasiman.net’);
    var osocialDataItems = [ olinkDataItem ];

    // Set up the post content
    var opostCreationData = new SP.Social.SocialPostCreationData();
    opostCreationData.set_contentText(‘The text for the post, which contains a {0}.’);
    opostCreationData.set_contentItems(osocialDataItems);

    // Write the post
    oresultThread = ofeedManager.createPost(null, opostCreationData);
    oclientContext.executeQueryAsync(WriteSucceeded, WriteFailed);
}

function WriteSucceeded(sender, args) {
    $get(“ResultMessage”).innerText = ‘Successfully posted the message to Posts’;
}
function WriteFailed(sender, args) {
    $get(“ResultMessage”).innerText = ‘Failure in writing message’ + args.get_message();
}

Open the default.aspx and add the following snippet

<Div id=”ResultMessage” style=”color: blue”></Div>

Open the AppManifest.XML and provide FullControl permission to UserProfiles (Social) and Tenant. Otherwise, you’ll get a permission error.

<App xmlns=”http://schemas.microsoft.com/sharepoint/2012/app/manifest”
     Name=”PublishPost”
     ProductID=”{72faf665-bf93-4a8e-88a7-2d2ad0be0f9d}”
     Version=”1.0.0.0″
     SharePointMinVersion=”15.0.0.0″
>
  <Properties>
    <Title>PublishPost</Title>
    <StartPage>~appWebUrl/Pages/Default.aspx?{StandardTokens}</StartPage>
  </Properties>

  <AppPrincipal>
    <Internal />
  </AppPrincipal>
  <AppPermissionRequests>
    <AppPermissionRequest Scope=”
http://sharepoint/social/tenant” Right=”FullControl” />
    <AppPermissionRequest Scope=”
http://sharepoint/content/tenant” Right=”FullControl” />
  </AppPermissionRequests>
</App>

Now, you’ll see that the post being published to MySite.

social post

 Subscribe to my blog

How to read SharePoint 2013 User Profile using JSOM

In this article i ‘b be dealing with how to read SharePoint 2013 User Profile properties using Javascript Object Model (JSOM). We know that SharePoint 2013 provides JSOM support for User Profile properties.

Create a SharePoint Hosted App and call it as ‘Read User Profile’

Go to Default.aspx in the SP Hosted App and add the references to SP.UserProfile.Js in the PlaceHolderAdditionalPageHead.

<script type="text/javascript" src="/_layouts/15/SP.UserProfiles.js"></script> 

It will already have references to SP.UserProfile.js and App.js

<script type="text/javascript" src="/_layouts/15/sp.js"></script>
<script type="text/javascript" src="../Scripts/App.js">

Open the App.JS and replace its current code with the following code

var personProperties;
// This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model
$(document).ready(function () {
    //    getUserName();
   
    SP.SOD.executeOrDelayUntilScriptLoaded(getUserProperties, ‘SP.UserProfiles.js’);
   
});

function getUserProperties() {

   
    //set your user id, the following format is the user id for Office 365. If you are reading it from SharePoint OnPremise assign user id in the DomainUserId
    var targetUser = “i:0#.f|membership|UserID@SharePointSite.onmicrosoft.com”;

    //Instantiate Client Context
    var clientContext = new SP.ClientContext.get_current();

    //Instantiate People Manager
    var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);

    // Get the instance of person properites
        personProperties = peopleManager.getPropertiesFor(targetUser);

    // Load the PersonProperties object and send the request.
    clientContext.load(personProperties);
    clientContext.executeQueryAsync(onRequestSuccess, onRequestFail);
}

// This function runs if the executeQueryAsync call succeeds.
function onRequestSuccess() {

    // Get a property directly from the PersonProperties object.
    var messageText = ” “LastName is ” property is “
        + personProperties.get_userProfileProperties()[‘LastName’];

    // Get a property from the UserProfileProperties property.
    messageText += “<br />”WorkPhone Number” property is “
        + personProperties.get_userProfileProperties()[‘WorkPhone’];
    $get(“results”).innerHTML = messageText;
}

// This function runs if the executeQueryAsync call fails.
function onRequestFail(sender, args) {
    $get(“results”).innerHTML = “Error: ” + args.get_message();
}

Set the following permissions

app permission

ReadUserProfile

 

 Subscribe to my blog

 

 

How to add a Calender App to Office 365 E3 public SharePoint Site

In this article, I’d deal with how to add a Calender App to Office 365 E3 public SharePoint site.  I came across this scenario when I was designing an Office 365 public facing site for the User Group in Chennai. When you subscribe for an Office 365 E3 subscription, you’ll get a public facing Sharepoint site.

Office 365 E3 Calender1

You’ll see a Calender App missing in the Site Contents when you try to add an App. You need to employ an work around to achieve this one.

Add Custom List App

Office 365 E3 Calender2

Go to List Settings –> Advanced Settings –> Enabled Management of Content Types

Office 365 E3 Calender3

Add from existing Site Content Types

Office 365 E3 Calender4

Click Ok.

Change new button order and default content type and set Event as default content type

Office 365 E3 Calender5

Now we have added the Calender App and we create events here.

 

 

 Subscribe to my blog

Deprecation of Custom Managed Code in Sandbox Solutions

Microsoft SharePoint product team has officially clarified yesterday that they are deprecating managed code in Sandbox Solutions.

http://blogs.msdn.com/b/sharepointdev/archive/2014/01/14/deprecation-of-custom-code-in-sandboxed-solutions.aspx

This will impact us if we are working on migration to Office 365 SharePoint online.

If you have customization in existing SP 2010 environment, please consider the following alternatives if you want to move to Cloud.

http://blogs.msdn.com/b/richard_dizeregas_blog/archive/2013/07/16/app-approaches-to-common-sharepoint-customizations.aspx

 Subscribe to my blog

How to set Profile Picture in Office 365 SharePoint 2013 site using CSOM

Step1

Create a console application in Visual Studio 2013.

Step2

Import the following namespaces

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.UserProfiles;

using System.Net;
using System.Security;
using System.Globalization; 

Step3

Add the following references

Step4

            Uri oUri = new Uri("https://yoursite.sharepoint.com");

            Office365ClaimsHelper claimsHelper = new Office365ClaimsHelper(oUri, "youruserid@yoursite.onmicrosoft.com", "password");
            using (ClientContext oClientContext = new ClientContext(oUri))
            {
                oClientContext.ExecutingWebRequest += claimsHelper.clientContext_ExecutingWebRequest;
                PeopleManager oPeopleManager = new PeopleManager(oClientContext);
                
                Stream oStream = new FileStream(@"C:MyPhotoSundar.jpg", FileMode.Open);
                oPeopleManager.SetMyProfilePicture(oStream);
                oClientContext.ExecuteQuery();
                Console.Write("Profile picture is set");



                Console.ReadLine();
            }

How to read UserProfile property from Office 365 SharePoint 2013 site using CSOM

Step#1

Create a console application in Visual Studio 2013

Step#2

Import the following namespaces

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Taxonomy;

using System.Net;
using System.Security;
using System.Globalization;

Step#3

Add references to the following .dll

references

Step#3
 static void Main(string[] args)
        
        {
            ReadUserProfile();
            
           
        }

        public static void ReadUserProfile()
        {
            Uri oUri = new Uri("https://yoursite.sharepoint.com");

            Office365ClaimsHelper claimsHelper = new Office365ClaimsHelper(oUri, "youruserid@yoursite.onmicrosoft.com", "password");
            using (ClientContext oClientContext = new ClientContext(oUri))
                        {
                            oClientContext.ExecutingWebRequest += claimsHelper.clientContext_ExecutingWebRequest;
                PeopleManager oPeopleManager = new PeopleManager(oClientContext);
                PersonProperties oPersonProperties = oPeopleManager.GetPropertiesFor("i:0#.f|membership|youruserid@yoursite.onmicrosoft.com");
                
                oClientContext.Load(oPersonProperties, p => p.AccountName, p => p.UserProfileProperties);
                oClientContext.ExecuteQuery();

                Console.WriteLine(oPersonProperties.UserProfileProperties.Count.ToString());
                
                foreach (var oProperty in oPersonProperties.UserProfileProperties)
                {
                    Console.WriteLine(string.Format("{0}: {1}",
                        oProperty.Key.ToString(), oProperty.Value.ToString()));
                    
                }
                Console.ReadLine();

            }

CSOM userprofile

The account parameter to be passed for the method oPeopleManager.GetPropertiesFor is not straight-forward. For office 365 SharePoint accounts, the parameter has to be passed in this format "i:0#.f|membership|youruserid@yoursite.onmicrosoft.com". Replace the youruserid accordingly.

The complete source code for this article is available in this location

 Subscribe to my blog

Visual Studio 2013 Community Launch @ Chennai–Dec 21, 2013

Date/Time:
Saturday, December 21, 2013 from 9:30 AM to 1:00 PM (PST)

Location:
Chennai

471, 2nd floor, Prestige Polygon, Anna Salai
Rathna Nagar, Teynampet
Chennai, 600018
India

Agenda & Schedule

No Agenda Schedule
1 Registration 9:30 – 10:00 AM
2 Visual Studio Studio 2013 IDE Changes and what’s new in .NET 4.5.1 10:00 AM – 11:00 AM
3 Tea Break 11:00 AM – 11:15 AM
4 Web Development in Visual Studio 2013 11:15 AM – 12:15 PM
5 Build HTML 5 based LOB Apps with Light Switch 12:15 PM – 1:00 PM
6 Q&A 1:00 AM – 1:15 AM

 Subscribe to my blog

Useful resources on SharePoint 2013 Capacity Planning

I’ve collated a list of resources that comes handy when we do Capacity Planning and Sizing for SharePoint Server 2013. The list of resources are below :-

1.Hardware and software requirements for SharePoint 2013
http://technet.microsoft.com/en-us/library/cc262485.aspx

2. Enterprise scale farms for SharePoint Server 2013
http://www.microsoft.com/en-us/download/details.aspx?id=35569

3. Streamlined topologies for SharePoint Server 2013
http://www.microsoft.com/en-us/download/details.aspx?id=37000

4. Traditional topologies for SharePoint Server 2013
http://www.microsoft.com/en-us/download/details.aspx?id=30377

5.Plan and use the Distributed Cache Service in SharePoint Server 2013
http://www.microsoft.com/en-us/download/details.aspx?id=35557

6.Plan for feeds and the Distributed Cache Service in SharePoint Server 2013
http://technet.microsoft.com/en-in/library/jj219572.aspx

7.Enterprise Search architectures for SharePoint Server 2013
http://www.microsoft.com/en-us/download/details.aspx?id=30383

8.Internet sites search architectures for SharePoint Server 2013
http://www.microsoft.com/en-us/download/details.aspx?id=30464

9.Scale search for performance and availability in SharePoint Server 2013
http://technet.microsoft.com/en-us/library/jj219628.aspx

10.Capacity Planning, Sizing and High Availability for Search in SPC172
http://social.technet.microsoft.com/wiki/contents/articles/16002.sharepoint-2013-capacity-planning-sizing-and-high-availability-for-search-in-spc172.aspx

11.Configuring a highly available workflow in workflow manager
http://technet.microsoft.com/en-us/library/jj193534

12.Use best practice for configurations for the SharePoint 2013 virtual machines and Hyper-V environment
http://technet.microsoft.com/en-us/library/ff621103.aspx

13.Plan Office Web Apps Server
http://technet.microsoft.com/en-us/library/jj219435.aspx

14.Estimate performance and capacity requirements for enterprise intranet collaboration environments (SharePoint Server 2013)
http://technet.microsoft.com/en-in/library/ff758657.aspx

15.Estimate capacity and performance for Web Content Management (SharePoint Server 2013)
http://technet.microsoft.com/en-us/library/d819b0d0-aa83-4e40-82d8-7a32195cc669(v=office.15).aspx

16.Estimate capacity and performance for Managed Metadata Service
http://technet.microsoft.com/en-in/library/gg681889.aspx

17.Estimate capacity and performance for video content management in SharePoint Server 2013
http://technet.microsoft.com/en-us/library/dn169054.aspx

18.Estimate capacity and performance for compliance and eDiscovery for SharePoint Server 2013
http://technet.microsoft.com/en-us/library/dn169053.aspx

 Subscribe to my blog

Microsoft Community Day–Nov 9

Tomorrow, Nov 9 is the Microsoft Community Day, there are whole lot of community activities planned around latest Microsoft technologies. 

The following webcasts are scheduled.

Working with Windows Azure IaaS

2:00PM IST

Register

What’s new in CRM 2013

3:00PM IST

Register

ONE ASP.NET

4:00PM IST

Register

Developing Near Field Communication in Windows Phone 8

5:00PM IST

Register

Windows 8 and Hyper-V

6:00PM IST

Register

Exchange Server 2013: Modern Public Folders

8:00PM IST

Register

Working with SharePoint 2013

9:00PM IST

Register

The following chat sessions are also scheduled

8103_image30_thumb_37F6FF58

If you are interested, please register here  http://blogs.technet.com/b/southasiamvp/archive/2013/11/04/community-day-activities.aspx

 Subscribe to my blog

Retrieve Person or Group field using SharePoint 2013 Client Object Model

I’m involved in troubleshooting a crazy people picker field issue in SharePoint 2013, which necessitates me to programmatically query the Person or Group field of SharePoint list using Client Object model. I was trying to figure out the exact syntax that can be used to grab the Person or Group field using FieldUserValue objects. Here is the complete code below :-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Client;
using System.Net;

namespace ReadApproverNameField
{
    public class Program
    {
        static void Main(string[] args)
        {
            GetApproverDetails();
            Console.ReadLine();

        }

        public static void GetApproverDetails()
        {
            ClientContext clientContext = new ClientContext(http://servername/sites/sitecollectionname);
            
            NetworkCredential oNetworkCredential = new NetworkCredential();
            oNetworkCredential.Domain = @"Domain Name";
            oNetworkCredential.UserName = @"User Id";
            oNetworkCredential.Password = @"Password";
            clientContext.Credentials = oNetworkCredential;

            List list = clientContext.Web.Lists.GetByTitle("Name of the list");

            CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml = "<View/>";
            ListItemCollection listItems = list.GetItems(camlQuery);
            clientContext.Load(list); 
            clientContext.Load(listItems);
            clientContext.ExecuteQuery();


            foreach (ListItem oListItem in listItems)
            {
                
                FieldUserValue name = oListItem.FieldValues["ApproverName"] as FieldUserValue;
                string person = name.LookupValue;                
                Console.WriteLine(oListItem.Id + "     " + oListItem["Title"].ToString() + "     " + person);


            }
        }
    }
}
Basically we cannot create a new instance of FieldUserValue object, it needs to be casted from the current item’s field using ‘as FieldValue’ syntax
and then the LookupValue need to be invoked to fetch the value. If we run the above code, we’ll get the output below.
pic1

 Subscribe to my blog