SharePoint 2013获取用户在JavaScript中创建的文档库

SharePoint 2013 get document library created by users in JavaScript

本文关键字:创建 文档 JavaScript 2013 获取 用户 SharePoint      更新时间:2023-09-26

嗨,我正试图获得仅由登录用户创建的所有文档库。使用下面的代码,我也得到了不是由用户创建的库。谢谢你。

function GetAllLibraries() {
    var listCollection = lists.getEnumerator();
    while (listCollection.moveNext()) {
        var listName = listCollection.get_current().get_title('Title');
        document.getElementById('leftDiv').innerHTML += "<b>" + listName + "<b/>" + "<br />";
    }
}

因为你正在使用SharePoint JavaScript API(又名JSOM),这是有点棘手,因为SP.List object不暴露Author属性来确定谁创建了这个对象。但好消息是,Author性质可以从SP.List.schemaXml property中提取,如下图所示

下面是如何检索当前用户

创建的列表的完整示例
var ctx = SP.ClientContext.get_current();
var allLists = ctx.get_web().get_lists();
var currentUser = ctx.get_web().get_currentUser();
ctx.load(allLists,'Include(SchemaXml)');
ctx.load(currentUser);
ctx.executeQueryAsync(
   function(){

      var lists = allLists.get_data().filter(function(list){
          var listProperties = schemaXml2Json(list.get_schemaXml()); 
          var listAuthorId = parseInt(listProperties.Author);
          return listAuthorId == currentUser.get_id(); 
      }); 
      console.log("The amount of lists created by current user: " + lists.length);       
   },
   logError);   
}

function schemaXml2Json(schemaXml)
{ 
    var jsonObject = {};
    var schemaXmlDoc = $.parseXML(schemaXml);
    $(schemaXmlDoc).find('List').each(function() {
      $.each(this.attributes, function(i, attr){
           jsonObject[attr.name] = attr.value;
      });
    });
    return jsonObject;
}


function logError(sender,args){
    console.log(args.get_message());
}

如果您想知道谁创建了列表或库,您需要获得SPList.Author属性。据我所知,你无法通过JSOM得到它。

我的建议是在服务器端开发自己的http处理程序,并通过ajax调用它。例如,你将参数传递给处理程序,如web url (_spPageContextInfo.webAbsoluteUrl),当前用户登录或id (_spPageContextInfo.userId),并在处理程序中迭代web上的列表,比较当前用户和列表创建者。最后,返回所需列表info.

或者只是开发web部件并做同样的事情:迭代列表并将其与SPContext.Current.Web.CurrentUser进行比较

<标题>更新:

c#代码示例。你可以把它放在你的web部件或事件处理程序中。在此代码中,我们迭代SPWeb上的所有列表,并保存当前用户创建的列表标题。

private void GetLists()
{
    using (SPSite site = new SPSite("{site_url}"))
    {
        using (SPWeb web = site.OpenWeb())
        {
            SPListCollection listCol = web.Lists;
            List<string> currentUserLists = new List<string>();
            foreach(SPList list in listCol)
            {
                if (list.Author.ID == SPContext.Current.Web.CurrentUser.ID)
                {
                    currentUserLists.Add(list.Title);
                }
            }
        }
    }
}