微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

    在《用Javascript获取SharePoint当前登录用户的用户名及Group信息》中已经介绍了用Javascript调用WebService获取SharePoint中当前登录用户信息的方法。如果要在部署到SharePoint里的Silverlight程序中获取当前登录SP的用户信息,可以直接调用宿主html页面中的javascript代码来实现:

  1. String userName = System.Windows.browser.HtmlPage.Window.Invoke("getCurrentUserName"nullas string;

      注:当javascript函数返回的是一个Array时,我不知道在Silverlight 2 Release中怎么获取返回值。我尝试过用string[]、Array()、List、Dictionary等类型接受,返回的都是null。

 

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

 

一、宿主页HTML:

  1. <Script language="javascript>
  2.    function getlogonedUserId()
  3.    {
  4.        return _spUserId;
  5.    }
  6. </Script>

二、在Silverlight里调用javascript来获取登录用户ID:

  1. double _userID = (double)HtmlPage.Window.Invoke("getlogonedUserId");

定义一个类SPUserInfo,负责根据“用户ID”来获取用户的信息(用户名及组):

  1. public class SPUserInfo
  2.     {
  3.         public double UserID { getset; }
  4.         public string UserName { getset; }
  5.         public List<string>  Groups { getset; }
  6.         internal event RoutedEventHandler GetUserInfoCompleted;
  7.         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>";
  8.         string soapPostfix = "</soap:Body></soap:Envelope>";
  9.         public SPUserInfo()
  10.         {
  11.             UserName = null;
  12.             Groups = new List<string>();
  13.         }
  14.         public void GetUserInfo(double userId)
  15.         {
  16.             UserID = userId;
  17.             GetUserName(); 
  18.         }
  19.         private void GetUserName()
  20.         {            
  21.             // Create the xml request for the list
  22.             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}",
  23.                 soapPrefix,
  24.                 UserID,
  25.                 soapPostfix);
  26.             // Todo: We need to dynamically generate the endpoint address using HtmlPage.Document.DocumentUri.
  27.             // Send data to Sharepoint List
  28.             WebClient client = new WebClient();
  29.             client.Headers["Content-Type"] = "text/xml; charset=utf-8";
  30.             client.Headers["SOAPAction"] = "http://schemas.microsoft.com/sharepoint/soap/GetListItems";
  31.             client.UploadStringCompleted += new UploadStringCompletedEventHandler(GetUserNameCompleted);
  32.             client.UploadStringAsync(new Uri( "http://yewen/_vti_bin/Lists.asmx?op=GetListItems", UriKind.Absolute), request);       // should be the root url of sharepoint
  33.         }
  34.         void GetUserNameCompleted(object sender, UploadStringCompletedEventArgs e)
  35.         {
  36.             if (e.Error == null)
  37.             {
  38.                 XElement root = XElement.Parse( e.Result );
  39.                 foreach (XElement element in root.Descendants("{#RowsetSchema}row"))
  40.                 {
  41.                     UserName = (string)element.Attribute("ows_Name");
  42.                     GetGroups();                    
  43.                 }
  44.             }
  45.         }
  46.         private void GetGroups()
  47.         {
  48.             // Create the xml request for the list
  49.             string request = String.Format("{0}<GetGroupCollectionFromUser xmlns=/"http://schemas.microsoft.com/sharepoint/soap/directory//"><userLoginName>{1}</userLoginName></GetGroupCollectionFromUser>{2}",
  50.                 UserName,
  51.                 soapPostfix);
  52.             // Todo: We need to dynamically generate the endpoint address using HtmlPage.Document.DocumentUri.
  53.             // Send data to Sharepoint List
  54.             WebClient client = new WebClient();
  55.             client.Headers["Content-Type"] = "text/xml; charset=utf-8";
  56.             client.Headers["SOAPAction"] = "http://schemas.microsoft.com/sharepoint/soap/directory/GetGroupCollectionFromUser";
  57.             client.UploadStringCompleted += new UploadStringCompletedEventHandler(GetGroupsCompleted);
  58.             client.UploadStringAsync(new Uri( "http://yewen/graceland/_vti_bin/UserGroup.asmx?op=GetGroupCollectionFromUser", request);  
  59.         }
  60.         // BAD CODES HERE, Should replace with other ways
  61.         void GetGroupsCompleted(object sender, UploadStringCompletedEventArgs e)
  62.         {
  63.             if (e.Error == null)
  64.             {
  65.                 XElement root = XElement.Parse(e.Result);
  66.                 foreach (XElement element in root.Elements())
  67.                 {
  68.                     foreach (XElement x2 in element.Descendants())
  69.                     {
  70.                         foreach (XElement x3 in x2.Descendants())
  71.                         {
  72.                             foreach (XElement x4 in x3.Descendants())
  73.                             {
  74.                                 foreach (XElement x5 in x4.Descendants())
  75.                                 {
  76.                                     foreach (XElement x6 in x5.Descendants())
  77.                                     {
  78.                                         Groups.Add(x6.Attribute("Name").ToString());
  79.                                     }
  80.                                 }
  81.                             }
  82.                         }                        
  83.                     }
  84.                 }
  85.                 //XDocument root = XDocument.Parse(e.Result);
  86.                 //XDocument doc = XDocument.Parse(e.Result.ToString());
  87.                 //var ret = from item in doc.Descendants("GetGroupCollectionFromUserResponse").ToList()
  88.                 //                  select new 
  89.                 //                  {
  90.                 //                      gp = (string)item.Attribute("Name").Value
  91.                 //                  };
  92.                 //foreach (object str in ret)
  93.                 //{
  94.                 //    Groups.Add(str.ToString()); 
  95.                 //}
  96.                 RaiseGetUserInfoCompleted();
  97.                
  98.             }
  99.         }
  100.         private void RaiseGetUserInfoCompleted()
  101.         {
  102.             if (GetUserInfoCompleted != null)
  103.                 GetUserInfoCompleted(thisnull);
  104.         }
  105.     }

    注: 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来获取当前登录用户的信息:

  1. SPUserInfo curUserInfo = new SPUserInfo();
  2. curUserInfo.GetUserInfoCompleted += new RoutedEventHandler(curUserInfo_GetUserInfoCompleted);
  3. curUserInfo.GetUserInfo(_userID);
  4. void curUserInfo_GetUserInfoCompleted(object sender, RoutedEventArgs e)
  5. {
  6.    //可以直接调用curUserInfo
  7.    //或者 SPUserInfo spU = sender as SPUserInfo      
  8. }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐