2016-08-15 45 views
0

嗨我試圖讓所有文檔庫只由記錄的用戶創建。使用下面的代碼我也得到了不是從用戶創建的庫。謝謝。SharePoint 2013獲取用戶在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 />"; 
    } 
} 

回答

0

如果你想知道是誰創建了列表或庫,你需要獲得屬性SPList.Author。據我所知,你不能通過JSOM得到它。

我對你的建議是在服務器端用邏輯開發你自己的http hanlder,並由ajax調用它。例如,您將參數傳遞到web url(_spPageContextInfo.webAbsoluteUrl),當前用戶登錄名或id(_spPageContextInfo.userId)等處理程序中,並在Web上的處理程序迭代列表中比較當前用戶和列表創建者。最後,返回需要的列表信息。

或者只是開發Web部件,做同樣的:迭代名單,並將其與SPContext.Current.Web.CurrentUser

UPDATE比較:的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); 
       } 
      } 
     } 
    } 
} 
+0

謝謝你的回覆。關鍵是,我得到了其他不屬於任何用戶的列表。這些列表在SharePoint Designer中甚至不可見,而我在SharePoint世界中是新的。 – Doro

+0

因此,我的建議是開發自定義Web部件或http處理程序,它將返回您想要的當前或特定用戶創建的列表。如果這是您的第一個共享點開發案例,請參閱我的文章中的更新,並轉至msdn [演練創建Web部件](https://msdn.microsoft.com/en-us/library/ee231551(v = vs.120)的.aspx)。 –

1

既然你正在使用SharePoint JavaScript API(a.k.a 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()); 
} 
相關問題