Accessing columns in the User Information List

 

I was recently asked how to programmatically retrieve custom fields from the User Information List via a Web service.

It turns out that the GetUserInfo web method does not return any custom fields that you might add to the User Information List (aka People & Groups).

Instead, just treat this as a regular SharePoint list and you can access the columns via theGetListItems web method fromLists.asmx.

static void Main(string[] args) 
{ 
    ListsService.Lists svcLists = new ListsService.Lists(); 
    svcLists.Credentials = CredentialCache.DefaultCredentials; 
    XmlDocument doc = new XmlDocument(); 
    doc.LoadXml("<Document><Query /><ViewFields /><QueryOptions /></Document>"); 
    XmlNode listQuery = doc.SelectSingleNode("//Query"); 
    XmlNode listViewFields = doc.SelectSingleNode("//ViewFields"); 
    XmlNode listQueryOptions = doc.SelectSingleNode("//QueryOptions"); 
    Guid g = GetWebID("http://moss.litwareinc.com");
    System.Xml.XmlNode items = svcLists.GetListItems("User Information List",
        string.Empty, listQuery, listViewFields, string.Empty, listQueryOptions,                 g.ToString()); 
    Console.WriteLine(items.OuterXml); 
}
private static Guid GetWebID(string webPath)
{ 
    SiteDataService.SiteData svcSiteData = new SiteDataService.SiteData(); 
    svcSiteData.UseDefaultCredentials = true; 
    SiteDataService._sWebMetadata webMetaData; 
    SiteDataService._sWebWithTime[] arrWebWithTime; 
    SiteDataService._sListWithTime[] arrListWithTime; 
    SiteDataService._sFPUrl[] arrUrls; 
    string roles; 
    string[] roleUsers; 
    string[] roleGroups; 
    svcSiteData.Url = webPath + "/_vti_bin/sitedata.asmx"; 
    uint i = svcSiteData.GetWeb(out webMetaData, out arrWebWithTime, 
        out arrListWithTime, out arrUrls, out roles, out roleUsers, 
        out roleGroups); 
    Guid g = new Guid(webMetaData.WebID); 
    return g; 
}


Now if I add a column to the User Information List, that new column appears in the items list.

One other option is RSS. The User Information List has an RSS feed and you can configure it to include your custom columns. Of course, this only shows new records and is not useful for export or update.

http://blogs.msdn.com/b/ben_hickman/archive/2009/05/22/accessing-columns-in-the-user-information-list.aspx

 

You can use the lists web service to query the user information list to get the user's login name. The code below takes the user's ID and returns the value for the ows_Name attribute which represents the user's login name.

public static string GetUserLogin(string ID)
 {

      string query = "<mylistitemrequest><Query><Where><Eq><FieldRef Name=\"ID\" /><Value Type=\"Counter\">@!ID</Value></Eq>";
      query += "</Where></Query>";
      query += "<ViewFields><FieldRef Name=\"Title\" /><FieldRef Name=\"Email\"/></ViewFields>";
      query += "<QueryOptions><ViewAttributes Scope=\"Recursive\"/></QueryOptions></mylistitemrequest>";

      query = query.Replace("@!ID", ID);

      XmlDocument doc = new XmlDocument();
      doc.LoadXml(query);


      listservice.Lists listProxy = new listservice.Lists();

      listProxy.Url = "http://basesmcdev2/sites/tester1/_vti_bin/lists.asmx";
      listProxy.UseDefaultCredentials = true;

      XmlNode queryNode = doc.SelectSingleNode("//Query");
      XmlNode viewNode = doc.SelectSingleNode("//ViewFields");
      XmlNode optionNode = doc.SelectSingleNode("//QueryOptions");

      XmlNode retNode = listProxy.GetListItems("user information list", string.Empty, queryNode, viewNode, string.Empty, optionNode, "e5bc34ff-6cde-4fee-aa4c-356baa57b37b");

      if (retNode != null)
      {
        XElement el = XElement.Parse(retNode.InnerXml).DescendantNodes().First() as XElement;
        return el.Attribute("ows_Name").Value.ToString();
      }

      return string.Empty;
    
}


http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/61645e0a-7e95-434a-ba04-54d399f501f4
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值