如何:使用 Web 服务修改用户配置文件数据
SharePoint 2010 其他版本 Office 2007
借助 Microsoft SharePoint Server 2010,您可以通过使用用户配置文件服务 Web 服务来更新用户配置文件数据。
以下示例将向您演示如何更新用户的家庭电话号码。执行代码之前,您必须向用户配置文件服务 Web 服务添加 Web 引用。
该 Web 服务位于 http://<site URL>/_vti_bin/userprofileservice.asmx。
使用此代码之前,请用实际的值替换 domainname、username 和电话号码 (nnnnnnnnnn)。另外在 Microsoft Visual Studio 项目中添加以下项的 Web 引用:
userprofileservice
示例
--------------------------------------------------------------------------------
复制 using System;
using System.Collections.Generic;
using System.Text;
namespace UserProfileWebServiceApp
{
class Program
{
public static localhost.UserProfileService myService =
new localhost.UserProfileService();
UserProfileWebService.localhost.PropertyData[] newdata =
new UserProfileWebService.localhost.PropertyData[1];
newdata[0] =
new UserProfileWebService.localhost.PropertyData();
newdata[0].Name = "HomePhone";
newdata[0].Values = new ValueData[1];
newdata[0].Values[0] = new ValueData();
newdata[0].Values[0].Value = "nnnnnnnnnnn";
newdata[0].IsValueChanged = true;
myService.ModifyUserPropertyByAccountName("domainname\\username",
newdata);
}
}
下面的代码示例演示如何使用对象模型创建用户配置文件和组织配置文件。在运行代码示例之前,请将 servername 替换为实际值。
另外在您的 Microsoft Visual Studio 项目中添加以下引用:
Microsoft.Office.Server
Microsoft.Office.Server.UserProfiles
Microsoft.SharePoint
System.Web
示例
--------------------------------------------------------------------------------
此示例创建一个用户配置文件。
复制 using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
namespace CreateUserProfile
{
class Program
{
static void Main(string[] args)
{
using (SPSite site = new SPSite("http://<servername>"))
{
SPServiceContext context = SPServiceContext.GetContext(site);
ProfileSubtypeManager psm = ProfileSubtypeManager.Get(context);
// choose default user profile subtype as the subtype
string subtypeName = ProfileSubtypeManager.GetDefaultProfileName(ProfileType.User);
ProfileSubtype subType = psm.GetProfileSubtype(subtypeName);
UserProfileManager upm = new UserProfileManager(context);
// create a user profile and set properties
UserProfile profile = upm.CreateUserProfile("domain\\alias");
profile.DisplayName = "Display Name";
profile.ProfileSubtype = subType;
profile.Commit();
}
}
}
}
此示例创建一个组织配置文件
复制 using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
namespace CreateOrganization
{
class Program
{
static void Main(string[] args)
{
using (SPSite site = new SPSite("http://<servername>"))
{
SPServiceContext context = SPServiceContext.GetContext(site);
ProfileSubtypeManager psm = ProfileSubtypeManager.Get(context);
// choose default organization profile subtype as the subtype
string subtypeName = ProfileSubtypeManager.GetDefaultProfileName(ProfileType.Organization);
ProfileSubtype subType = psm.GetProfileSubtype(subtypeName);
OrganizationProfileManager opm = new OrganizationProfileManager(context);
// choose Root Organization as the parent
OrganizationProfile parentOrg = opm.RootOrganization;
// create an organization profile and set its display name
OrganizationProfile profile = opm.CreateOrganizationProfile(subType, parentOrg);
profile.DisplayName = "Test Org1";
// commit to save changes
profile.Commit();
}
}
}
}