Click here to Skip to main content
15,920,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public List<SPWeb> GetAllWebs(string webURL)
{
    string url = System.Web.HttpUtility.UrlDecode(webURL);

    List<SPWeb> webColl = new List<SPWeb>();
    try
    {
        ClientContext clientContext = new ClientContext(url);
        Web oWebsite = clientContext.Web;

        clientContext.Load(oWebsite,
            website => website.Webs,
            website => website.Title,
            website => website.Id,
            website => website.ServerRelativeUrl
            );
        clientContext.ExecuteQuery();

        foreach (Web oneWeb in oWebsite.Webs)
        {
            webColl.Add(new SPWeb
            {
                Title = oneWeb.Title,
                WebGUID = oneWeb.Id.ToString(),
                ServerRelativeUrl = (url + oneWeb.ServerRelativeUrl).Replace(@"\", ""),
            });
        }
    }
    catch (Exception ex)
    {
        // error log
        string error = ex.Message + " Error within GetAllWebs ";
    }
    return webColl;
}

public List<SPList> GetAllLibraries(string webURL)
{
    var listColl = new List<SPList>();
    ClientContext _ctx = new ClientContext(webURL);
    try
    {
        var currentWeb = _ctx.Web;
        var AllLists = currentWeb.Lists;
        _ctx.Load(AllLists);
        _ctx.ExecuteQuery();
        var query = from list in currentWeb.Lists
                    where list.BaseType == BaseType.DocumentLibrary
                    select list;

        var listCollection = _ctx.LoadQuery(query.Include(myList => myList.Title,
                                      myList => myList.Id,
                                      myList => myList.RootFolder.ServerRelativeUrl,
                                      myList => myList.ParentWebUrl,
                                      myList => myList.Hidden,
                                      myList => myList.IsApplicationList));
        _ctx.ExecuteQuery();

      //   /*

        listColl.AddRange( (from list in listCollection
                           where !list.Hidden
                           select new SPList
                           {
                               Title = list.Title,
                               ListGUID = list.Id.ToString(),
                               RootFolderServerRelativeUrl = list.RootFolder.ServerRelativeUrl,
                               ParentWebUrl = list.ParentWebUrl
                           //below Distinct not working
                           }).Distinct() );

   // } */
        foreach (var Item in listCollection)
        {
            listColl.Add(new SPList
            {
                Title = Item.Title,
                RootFolderServerRelativeUrl = Item.RootFolder.ServerRelativeUrl,
                ListGUID = Item.Id.ToString()
            });
        }
    }
    catch (Exception ex)
    {
        // error log
        string error = ex.Message + " Error within GetAllLibraries ";
    }
    //return error
    //return listColl.Distinct();
    return listColl;
}
Posted

1 solution

Hi Member,

Think it this way - how should the Default Distinct method "know" how to compare your objects (of type SPList) and then decide which are distinct? (it will use the Default comparer which will not work for you - maybe see the msdn documentation for Distinct?)

Just use the overload list.Distinct with an IEqulaityComparere or implement IEquatable for your SPList class.

http://msdn.microsoft.com/EN-US/library/bb348436(v=vs.110).aspx
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900