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.                 soapPrefix,
  51.                 UserName,
  52.                 soapPostfix);
  53.             // TODO: We need to dynamically generate the endpoint address using HtmlPage.Document.DocumentUri.
  54.             // Send data to Sharepoint List
  55.             WebClient client = new WebClient();
  56.             client.Headers["Content-Type"] = "text/xml; charset=utf-8";
  57.             client.Headers["SOAPAction"] = "http://schemas.microsoft.com/sharepoint/soap/directory/GetGroupCollectionFromUser";
  58.             client.UploadStringCompleted += new UploadStringCompletedEventHandler(GetGroupsCompleted);
  59.             client.UploadStringAsync(new Uri( "http://yewen/graceland/_vti_bin/UserGroup.asmx?op=GetGroupCollectionFromUser", UriKind.Absolute), request);  
  60.         }
  61.         // BAD CODES HERE, Should replace with other ways
  62.         void GetGroupsCompleted(object sender, UploadStringCompletedEventArgs e)
  63.         {
  64.             if (e.Error == null)
  65.             {
  66.                 XElement root = XElement.Parse(e.Result);
  67.                 foreach (XElement element in root.Elements())
  68.                 {
  69.                     foreach (XElement x2 in element.Descendants())
  70.                     {
  71.                         foreach (XElement x3 in x2.Descendants())
  72.                         {
  73.                             foreach (XElement x4 in x3.Descendants())
  74.                             {
  75.                                 foreach (XElement x5 in x4.Descendants())
  76.                                 {
  77.                                     foreach (XElement x6 in x5.Descendants())
  78.                                     {
  79.                                         Groups.Add(x6.Attribute("Name").ToString());
  80.                                     }
  81.                                 }
  82.                             }
  83.                         }                        
  84.                     }
  85.                 }
  86.                 //XDocument root = XDocument.Parse(e.Result);
  87.                 //XDocument doc = XDocument.Parse(e.Result.ToString());
  88.                 //var ret = from item in doc.Descendants("GetGroupCollectionFromUserResponse").ToList()
  89.                 //                  select new 
  90.                 //                  {
  91.                 //                      gp = (string)item.Attribute("Name").Value
  92.                 //                  };
  93.                 //foreach (object str in ret)
  94.                 //{
  95.                 //    Groups.Add(str.ToString()); 
  96.                 //}
  97.                 RaiseGetUserInfoCompleted();
  98.                
  99.             }
  100.         }
  101.         private void RaiseGetUserInfoCompleted()
  102.         {
  103.             if (GetUserInfoCompleted != null)
  104.                 GetUserInfoCompleted(thisnull);
  105.         }
  106.     }

    注: 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. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值