簡體   English   中英

SharePoint 2013獲取用戶在JavaScript中創建的文檔庫

[英]SharePoint 2013 get document library created by users in JavaScript

嗨,我正在嘗試獲取僅由登錄用戶創建的所有文檔庫。 通過以下代碼,我還獲得了不是由用戶創建的庫。 謝謝。

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 hanlder,並通過ajax調用它。 例如,您將參數傳遞到處理程序中,例如Web url( _spPageContextInfo.webAbsoluteUrl ),當前用戶登錄名或ID( _spPageContextInfo.userId ),並在處理程序中迭代Web上的列表,比較當前用戶和列表創建者。 最后,返回所需的列表信息。

或者只是開發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);
                }
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM