0

Hi I am trying to get all documents library only created by the logged users. With the following code I get also libraries which was not created from a user. Thank you.

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 />";
    }
}
Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193
Doro
  • 671
  • 1
  • 16
  • 34

2 Answers2

1

Since you are utilizing SharePoint JavaScript API (a.k.a JSOM) it is a bit tricky since SP.List object does not expose Author property to determine who created this object. But the good news that Author property could be extracted from SP.List.schemaXml property as demonstrated below

Here is a complete example how to retrieve lists created by current user

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());
}
Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193
0

If you want to know who created list or library, you need to get property SPList.Author. As i know, you can't get it by JSOM.

My advice for you is to develop your own http hanlder with logic on server-side and call it by ajax. For example, you pass arguments into handler like web url (_spPageContextInfo.webAbsoluteUrl), current user login or id (_spPageContextInfo.userId), and in handler iterate lists on web, compare current user and list creator. Finally, return needed lists info.

Or just develop web part and do the same: iterate lists and compare it with SPContext.Current.Web.CurrentUser

UPDATE:

Example of c# code. You can put it in your web part or event handler. In this code we iterate all lists on SPWeb and save lists title created by current user.

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);
                }
            }
        }
    }
}
Community
  • 1
  • 1
  • Thank you for the reply. The point is, I get other lists which are not part of any users. Those lists are not even visible in SharePoint Designer and I am new in SharePoint world. – Doro Aug 16 '16 at 05:51
  • So, my suggestion is to develop custom web part or http handler which will return lists created by current or particular user you want. If it's your first sharepoint development case, please see update in my post and go to msdn for [walkthrough creating web part](https://msdn.microsoft.com/en-us/library/ee231551(v=vs.120).aspx). – Semushin Dmitrii Aug 16 '16 at 07:15