Bamboo: Essentials for SharePoint Here is the SharePoint 2010 FeedBlitz email update for you.



SharePoint 2010 Cookbook: Programmer's Guide to User Profiles, Part 3 - Using a WCF Service to Update UserProfile Properties
  

This is the third in a four-part series of blog entries in which I will describe how to read and modify the SharePoint UserProfile using the SharePoint Object Model.

The series will consist of the following:

 

Challenge

Using a WCF Service in Microsoft SharePoint Server 2010, how do we update UserProfile properties? 

 

Solution

The main reason I wrote this article is because once I discovered the WCF Service in SharePoint 2010, the first thing I used the service for was to update UserProfile properties. The projects I've attached will show you how to do this, and will provide some clear examples.  This tutorial will not be a step-by-step guide to making the Windows Forms Applications that implement the service, as if you have the tutorial, you should also have the (attached) source code for the Tutorial project.  Notice that we have a Solution with two different projects in it. The first project is a WCF Service Library to get UserProfile object and update UserProfile properties (WCFWithAsync), and the second project is the Windows Forms Application that implements the service (UserDirectory).

 

1. Create a WCF Service in SharePoint Server to get and update UserProfile properties

Take a look at MSDN to learn how to go about Creating a Custom WCF Service in SharePoint Foundation.

So, the Solution Explorer in our case should look something like this:

 

There is very little code in this project. In fact, the only code we're worried about is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Microsoft.Office.Server.UserProfiles;
 
namespace WCFWithAsync.ISAPI
{
    [ServiceContract]
    public interface IUserDirectoryService
    {
        [OperationContract]
        MyUserProfile SearchUserProfile(string accountName);
 
        [OperationContract]
        bool UpdateUserProfile(MyUserProfile oMyUserProfile, string urlSite);
    }
 
    [DataContract]
    public class MyUserProfile
    {
        //Some properties in Part1
        [DataMember]
        public string AccountName { get; set; }
        [DataMember]
        public string FirstName { get; set; }
        [DataMember]
        public string LastName { get; set; }
        [DataMember]
        public string PreferredName { get; set; }
        [DataMember]
        public string Department { get; set; }
        [DataMember]
        public string Manager { get; set; }
        [DataMember]
        public string AboutMe { get; set; }
        [DataMember]
        public string SPS_Birthday { get; set; }
        [DataMember]
        public string CellPhone { get; set; }
    }
}

In this code, we have an Interface and a DataContract class declared. The Interface only has two methods to get and update UserProfile. The MyUserProfile class declared some properties that we'll be using in the examples.

The details on how to get UserProfile was covered in Part 2 of my series, and the way to update UserProfile properties is equally simple:

public bool UpdateUserProfile(MyUserProfile oMyUserProfile, string urlSite)
{
    bool flag = false;
    try
    {
        System.Security.PermissionSet ps = new System.Security.PermissionSet(System.Security.Permissions.PermissionState.Unrestricted);
        ps.Assert();
 
        using (Microsoft.SharePoint.SPSite site = new Microsoft.SharePoint.SPSite(urlSite))
        {
            Microsoft.SharePoint.SPServiceContext context = Microsoft.SharePoint.SPServiceContext.GetContext(site);
            Microsoft.Office.Server.UserProfiles.UserProfile userSave = GetUserProfileByAccountName(oMyUserProfile.AccountName, context);
 
            if (userSave != null)
            {
                UpdateUserProfileValue(userSave, oMyUserProfile.AccountName, "AccountName");
                UpdateUserProfileValue(userSave, oMyUserProfile.FirstName, "FirstName");
                UpdateUserProfileValue(userSave, oMyUserProfile.LastName, "LastName");
                UpdateUserProfileValue(userSave, oMyUserProfile.PreferredName, "PreferredName");
                UpdateUserProfileValue(userSave, oMyUserProfile.Department, "Department");
                UpdateUserProfileValue(userSave, oMyUserProfile.Manager, "Manager");
                UpdateUserProfileValue(userSave, oMyUserProfile.AboutMe, "AboutMe");
                UpdateUserProfileValue(userSave, oMyUserProfile.SPS_Birthday, "SPS-Birthday");
                UpdateUserProfileValue(userSave, oMyUserProfile.CellPhone, "CellPhone");
                flag = true;
            }
        }
    }
    catch (Exception ex)
    {
        //Write some error log here
    }
    finally
    {
        System.Security.CodeAccessPermission.RevertAssert();
    }
    return flag;
}
 
private void UpdateUserProfileValue( Microsoft.Office.Server.UserProfiles.UserProfile userSave, string propertyValue, string propertyName)
{
    try
    {
        string oldValue = this.GetValueOfUserProfileProperty(userSave, propertyName);
        if (oldValue != propertyValue)
        {
            userSave[propertyName].Value = propertyValue;
            userSave.Commit();
        }
    }
    catch (Exception ex)
    {
        //Write some error log here
    }
}

 

Note: Remember that you will need the admin permission to update UserProfile properties

That's all you need, please see my attached projects for more code samples. Then, deploy this WCF Service as follows:

http://illumination1:11111/sites/ConvertToO14/_vti_bin/UserDirectoryService/UserDirectoryService.svc

 

2. Checking anonymous authentication to the SharePoint site

Make sure the status of Anonymous Authentication is enabled.

Go to Administrator Tools and select Internet Information Service (IIS) Manager

 

Expand sites and click on SharePoint- 11111 (the SharePoint Site where we are going to deploy the WCF service)

 

Click Authentication as shown in the above figure.

 

Check whether the status of Anonymous Authentication is enabled.

 

3. Testing the Service with a Windows Forms Application

Take a look at MSDN to learn how to go about Creating a Windows Forms Application to Interact with List Data

In this post, I do the same way but using above WCF Service (UserDirectoryService)

The next bit of code I will introduce is the code that actually gets and updates UserProfile in Windows Forms Applications:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace UserDirectory
{
    using UserDirectoryService;
 
    public partial class Form1 : Form
    {
        private static string websiteUrl = "http://illumination1:11111/sites/ConvertToO14";
        private UserDirectoryServiceClient proxy;
 
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            System.ServiceModel.BasicHttpBinding binding = new System.ServiceModel.BasicHttpBinding();
            binding.Security.Mode = System.ServiceModel.BasicHttpSecurityMode.TransportCredentialOnly;
            binding.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Ntlm;
            System.ServiceModel.EndpointAddress endpoint = new System.ServiceModel.EndpointAddress(websiteUrl + "/_vti_bin/UserDirectoryService/UserDirectoryService.svc");
 
            proxy = new UserDirectoryServiceClient(binding, endpoint);
            proxy.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
        }
 
        private void btnSearch_Click(object sender, EventArgs e)
        {
            string strAccountName = txtSearch.Text.Trim();
            if (!string.IsNullOrEmpty(strAccountName))
            {
                object obj = proxy.SearchUserProfile(strAccountName);
 
                if (obj is MyUserProfile)
                {
                    MyUserProfile oMyUserProfile = obj as MyUserProfile;
                    myUserProfileBindingSource.DataSource = oMyUserProfile;
                }
            }
        }
 
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            MyUserProfile tempUserProfile = (MyUserProfile)myUserProfileBindingSource.Current;
            if (tempUserProfile != null)
            {
                bool isSuccess = proxy.UpdateUserProfile(tempUserProfile, websiteUrl);
                this.Text = "Updated for user: " + tempUserProfile.AccountName;
            }
        }
    }
}

 

Now, enjoy:

Enter any name in the textbox and click Search:

 

The Application will show the detail of that account name in the grid. You can edit his value on any column and click Update current user to update that UserProfile.

Click Search again to make sure that the update was successful.

 

See Also



More Recent Articles

 

Where's Bamboo?

Microsoft SharePoint Conference 2011


Click here to safely unsubscribe now from "SharePoint 2010" or change your subscription, view mailing archives or subscribe

Your requested content delivery powered by FeedBlitz, LLC, 9 Thoreau Way, Sudbury, MA 01776, USA. +1.978.776.9498