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

Silverlight获取SharePoint当前登录用户信息,第1张

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

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

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:

<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 { getset; }         public string Username { getset; }         public List<string>  Groups { getset; }         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(thisnull);         }     }

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

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       }
总结

以上是内存溢出为你收集整理的Silverlight获取SharePoint当前登录用户信息全部内容,希望文章能够帮你解决Silverlight获取SharePoint当前登录用户信息所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1053119.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存