Silverlight获取SharePoint当前登录用户信息

2008-10-27 14:35:51 来源:中国自学编程网 作者:佚名 点击:
如果要在部署到SharePoint里的Silverlight程序中获取当前登录SP的用户信息,可以直接调用宿主html页面中的javascript代码来实现:



如果要在部署到SharePoint里的Silverlight程序中获取当前登录SP的用户信息,可以直接调用宿主html页面中的javascript代码来实现:

String userName = System.Windows.Browser.HtmlPage.Window.Invoke("getCurrentUserName", null) as string;
注:当javascript函数返回的是一个Array时,我不知道在Silverlight 2 Release中怎么获取返回值。我尝试过用string[]、Array()、List、Dictionary等类型接受,返回的都是null。



但如果不想调用host页面的javascript,则也可以直接用C#代码来调用SharePoint的WebService来实现。但前提是必须通过调用Host页的HTML来获取_spUserId变量的值,即当前登录用户的ID。



一、宿主页HTML:

<Script language="javascript>
function getLogonedUserId()
{
return _spUserId;
}
</Script>
二、在Silverlight里调用javascript来获取登录用户ID:

double _userID = (double)HtmlPage.Window.Invoke("getLogonedUserId");
定义一个类SPUserInfo,负责根据“用户ID”来获取用户的信息(用户名及组):

public class SPUserInfo
{
public double UserID { get; set; }
public string UserName { get; set; }
public List<string> Groups { get; set; }

internal event RoutedEventHandler GetUserInfoCompleted;

string soapPrefix = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body>";
string soapPostfix = "</soap:Body></soap:Envelope>";

public SPUserInfo()
{
UserName = null;
Groups = new List<string>();
}

public void GetUserInfo(double userId)
{
UserID = userId;
GetUserName();
}

private void GetUserName()
{
// Create the xml request for the list
string request = String.Format("{0}<GetListItems xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\">

<listName>User Information List</listName><viewName></viewName><query><Query><Where>

<Eq><FieldRef Name=\"ID\"/><Value Type=\"Counter\">{1}</Value></Eq></Where></Query>

</query><viewFields><ViewFields><FieldRef Name=\"Name\"/></ViewFields></viewFields><rowLimit>1</rowLimit>

<queryOptions><QueryOptions/></queryOptions><webID></webID></GetListItems>{2}",
soapPrefix,
UserID,
soapPostfix);

// TODO: We need to dynamically generate the endpoint address using HtmlPage.Document.DocumentUri.
// Send data to Sharepoint List
WebClient client = new WebClient();
client.Headers["Content-Type"] = "text/xml; charset=utf-8";
client.Headers["SOAPAction"] = "http://schemas.microsoft.com/sharepoint/soap/GetListItems";
client.UploadStringCompleted += new UploadStringCompletedEventHandler(GetUserNameCompleted);
client.UploadStringAsync(new Uri( "http://yewen/_vti_bin/Lists.asmx?op=GetListItems", UriKind.Absolute), request); // should be the root url of sharepoint
}

void GetUserNameCompleted(object sender, UploadStringCompletedEventArgs e)
{
if (e.Error == null)
{
XElement root = XElement.Parse( e.Result );
foreach (XElement element in root.Descendants("{#RowsetSchema}row"))
{
UserName = (string)element.Attribute("ows_Name");
GetGroups();
}
}
}

private void GetGroups()
{
// Create the xml request for the list
string request = String.Format("{0}<GetGroupCollectionFromUser xmlns=\"http://schemas.microsoft.com/sharepoint/soap/directory/\"><userLoginName>{1}</userLoginName></GetGroupCollectionFromUser>{2}",
soapPrefix,
UserName,
soapPostfix);

// TODO: We need to dynamically generate the endpoint address using HtmlPage.Document.DocumentUri.
// Send data to Sharepoint List
WebClient client = new WebClient();
client.Headers["Content-Type"] = "text/xml; charset=utf-8";
client.Headers["SOAPAction"] = "http://schemas.microsoft.com/sharepoint/soap/directory/GetGroupCollectionFromUser";
client.UploadStringCompleted += new UploadStringCompletedEventHandler(GetGroupsCompleted);
client.UploadStringAsync(new Uri( "http://yewen/graceland/_vti_bin/UserGroup.asmx?op=GetGroupCollectionFromUser", UriKind.Absolute), request);
}


// BAD CODES HERE, Should replace with other ways
void GetGroupsCompleted(object sender, UploadStringCompletedEventArgs e)
{
if (e.Error == null)
{
9 7 3 1 2 4 8 :



本类最新行业评测技巧教程学院
本类热点本日本周本月
本类推荐本日本周本月

广告联系 | 版权说明 | 意见建议 | 加入收藏 | 军网站群 [ 军软件园 - 军软件商城 - 军软件园论坛 ]

电信与信息服务业务经营许可证:京ICP证050203