在《用Javascript获取SharePoint当前登录用户的用户名及Group信息》中已经介绍了用Javascript调用WebService获取SharePoint中当前登录用户信息的方法。如果要在部署到SharePoint里的Silverlight程序中获取当前登录SP的用户信息,可以直接调用宿主html页面中的javascript代码来实现:
注:当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}",
- 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", request);
- }
- // BAD CODES HERE, Should replace with other ways
- void GetGroupsCompleted(object sender, UploadStringCompletedEventArgs e)
- {
- if (e.Error == null)
- {
- XElement root = XElement.Parse(e.Result);
- foreach (XElement element in root.Elements())
- {
- foreach (XElement x2 in element.Descendants())
- {
- foreach (XElement x3 in x2.Descendants())
- {
- foreach (XElement x4 in x3.Descendants())
- {
- foreach (XElement x5 in x4.Descendants())
- {
- foreach (XElement x6 in x5.Descendants())
- {
- Groups.Add(x6.Attribute("Name").ToString());
- }
- }
- }
- }
- }
- }
- //XDocument root = XDocument.Parse(e.Result);
- //XDocument doc = XDocument.Parse(e.Result.ToString());
- //var ret = from item in doc.Descendants("GetGroupCollectionFromUserResponse").ToList()
- // select new
- // {
- // gp = (string)item.Attribute("Name").Value
- // };
- //foreach (object str in ret)
- //{
- // Groups.Add(str.ToString());
- //}
- RaiseGetUserInfoCompleted();
- }
- }
- private void RaiseGetUserInfoCompleted()
- {
- if (GetUserInfoCompleted != null)
- GetUserInfoCompleted(this, null);
- }
- }
注: 1) GetUserName() 中SOAPAction为“http://schemas.microsoft.com/sharepoint/soap/GetListItems”,而不是“http://schemas.microsoft.com/sharepoint/soap/directory/GetListItems”;
2) GetUserName()中调用的WebService为SharePoint根目录下的Lists.asmx “http://yewen/_vti_bin/Lists.asmx?op=GetListItems”,当我尝试调用对应的SharePoint下子站点的WebService “http://yewen/graceland/_vti_bin/Lists.asmx?op=GetListItems”时却出错,不知为何?
3)GetGroups()中却不用考虑上述两点;
4)void GetGroupsCompleted(object sender,UploadStringCompletedEventArgs e)中解析Group的代码很糟糕,应该用Linq以更好的方式来实现!!!!
- SPUserInfo curUserInfo = new SPUserInfo();
- curUserInfo.GetUserInfoCompleted += new RoutedEventHandler(curUserInfo_GetUserInfoCompleted);
- curUserInfo.GetUserInfo(_userID);
- void curUserInfo_GetUserInfoCompleted(object sender, RoutedEventArgs e)
- {
- //可以直接调用curUserInfo
- //或者 SPUserInfo spU = sender as SPUserInfo
- }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。