Programmatically Create,Retrieve User Profile,User Profile Properties in SharePoint 2013

Summary : Programmatically Create,Retrieve User Profile,User Profile Properties in SharePoint 2013,Get User Profiles and User Profile Properties in Sharepoint 2013,Create SocialActor Profile Property in SharePoint 2013

In SharePoint 2013 you can Programmatically Create and Retrieve a User Profile and User Profile Properties using the following APIs.

  • Client object models for managed code
  • .NET client object model
  • Silverlight client object model
  • Mobile client object model
  • JavaScript object model
  • Representational State Transfer (REST) service
  • Server object model
Because We’re So Good, Our Content gets Copied very Often.If you are not reading this on LearningSharePoint.com, please read the Original article Here for details, comments and updates on this post.



Each API includes a manager object that is used to perform core profile-related tasks.In addition to
User Profiles each user now has a Social Profile to track interact with Social activities or properties of a User. In addition to the existing ones in SharePoint 2013 Ms has Included the following new objects that represent Users and User Properties:

The SocialActor object represents users (and other entities) for feed,following and other social activities.

The PersonProperties object contains general user properties and user profile properties. PersonProperties is the primary API for accessing user properties from client-side code.

Lets look at a Server Object model example of how to Create a User Profile,Retrieve a User Profile and Create and Social Profile Properties for a User.

Classes used –

using Microsoft.Office.Server.Social;
using Microsoft.Office.Server.Administration;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint.Portal.UserProfiles;

//Declaring Objects

SPWeb currentWeb = SPContext.Current.Web;
SPUser currentUser = SPContext.Current.Web.CurrentUser;
SPServiceContext serverContext = SPServiceContext.GetContext(currentWeb.Site);

//Retrieve Current User’s UserProfile

UserProfileManager profileManager = new UserProfileManager(serverContext);
UserProfile profile = profileManager.GetUserProfile(currentUser.LoginName);
if (profile != null)

{
//Set the Profile
profile.DisplayName = “Robin”;
}

//Create a User Profile

UserProfileManager userProfileMgr = new UserProfileManager(serverContext);
UserProfile newUserProfile = userProfileMgr.CreateUserProfile(newAccountName);

//Set an Existing User Profile properties

newUserProfile.DisplayName = "New User";
newUserProfile.Commit();

//Create a Custom User Profile Property.

A Custom Profile property and its attributes are Created with a set of Objects: a CoreProperty object, a ProfileTypeProperty object, and a ProfileSubtypeProperty object.In this example we will Create a Custom Profile Property “Personal Blog” for a User.

ProfilePropertyManager profilePropMgr = new UserProfileConfigManager(serverContext).ProfilePropertyManager;
CorePropertyManager corePropMgr = profilePropMgr.GetCoreProperties();

// Create a Blog URL property.
CoreProperty coreProp = corePropMgr.Create(false);
coreProp.Name = "PersonalBlog";
coreProp.DisplayName = "Personal Blog";
coreProp.Type = PropertyDataType.URL;
coreProp.Length = 100;
corePropMgr.Add(coreProp);

// Make it Visible to User in Profile
ProfileTypePropertyManager typePropMgr = profilePropMgr.GetProfileTypeProperties(ProfileType.User);
ProfileTypeProperty typeProp = typePropMgr.Create(coreProp);
typeProp.IsVisibleOnViewer = true;
typePropMgr.Add(typeProp);

//Create a Social Profile with SPSocialFollowingManager

SPSocialFollowingManager _followManager = new SPSocialFollowingManager(profile);

//Create a Social actor of type Document

SPSocialActorInfo newActor = new SPSocialActorInfo();

newActor.ActorType = SPSocialActorType.Document;

//Follow the document actor
newActor.ContentUri = new Uri('path of document to follow');
_followManager.Follow(newActor);

Please Note that to Create or Change user profiles, you must use the server object model. User profiles and user profile properties from client APIs are read-only (except the user profile picture).

You might also want to see other SharePoint 2013 Developer Tutorials and Samples

Original article : Here


Speak Your Mind

*

Disclaimer: The contents of this site are provided “AS IS”. Information and views expressed including URL(s) and other Internet website references, may change without notice. You bear the risk of using it. SharePoint, MOSS, SharePoint 2010, SharePoint 2013 and Microsoft are trademarks of the Microsoft Corporation.Learning SharePoint only provides Tips,Tricks and Tutorials for using Microsoft SharePoint, Microsoft Office and related Softwares.
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.