Microsoft.SharePoint.Client C#仅获取用户创建的列表(而不是文档库)

时间:2018-02-07 19:58:29

标签: list sharepoint

我正在尝试从指定的网站检索用户生成的列表列表。我不想要系统生成的列表(例如MicroFeed)或文档库。使用Microsoft示例我有这个代码:

    public static void LoadLists(Microsoft.SharePoint.Client.Web web, List<String> foldersList)
    {
        var ctx = web.Context;
        ListCollection collList = web.Lists;

        IEnumerable<List> listInfo = ctx.LoadQuery(
            collList.Include(
                list => list.Title,
                list => list.Fields.Include(
                    field => field.Title,
                    field => field.InternalName)));

        ctx.ExecuteQuery();

        foreach (List oList in listInfo)
        {
            FieldCollection collField = oList.Fields;

            foreach (Microsoft.SharePoint.Client.Field oField in collField)
            {
                Regex regEx = new Regex("name", RegexOptions.IgnoreCase);

                if (regEx.IsMatch(oField.InternalName))
                {
                    Console.WriteLine("List: {0} \n\t Field Title: {1} \n\t Field Internal Name: {2}",
                        oList.Title, oField.Title, oField.InternalName);
                }
            }
        }
    }

然而,这将返回所有列表和文档库(天堂知道还有什么)。有没有一种简单的方法来取回用户定义的列表?这是我想要得到的一个例子:

enter image description here

查看Microsoft的文档,他们似乎使用术语列表来引用实际列表(表)和文档库(文件夹)。获取列表的正确命名法是什么,就像excel数据电子表格一样?最后,列表(表)是否可以嵌套在文档库的旁边?我似乎无法做到这一点,但我想检查,因为我不熟悉SharePoint。

谢谢!

1 个答案:

答案 0 :(得分:1)

因此,在必须查找大量示例(不是来自Microsoft,谢谢)并逐步通过实际响应之后,以下是仅加载用户创建的列表及其字段列(未隐藏)的代码。我确信这可以优化/清理(例如,不必运行辅助查询来获取List属性,但它在原始查询中给了我拒绝访问权限),但它对我有用。如果事情向南发展的话,还需要一些爱护的尝试。

首先要保存数据的几个类:

public class SharePointColumn
{
    public string Title { get; set; }
    public string InternalName { get; set; }
    public string TypeAsString { get; set; }
}

public class SharePointLibrary
{
    public SharePointLibrary()
    {
        Columns = new List<SharePointColumn>();
    }
    public string Title { get; set; }
    public Boolean IsList { get; set; } // If true a list, else DocumentLibrary
    public List<SharePointColumn> Columns { get; set; }
}

然后是真实的代码。

    public static void LoadLists(Microsoft.SharePoint.Client.Web web, List<SharePointLibrary> sharePointLibraries)
    {
        var ctx = web.Context;
        ListCollection collList = web.Lists;

        IEnumerable<List> listInfo = ctx.LoadQuery(
            collList.Include(
                list => list.Title,
                list => list.Fields.Include(
                    field => field.Title,
                    field => field.InternalName,
                    field => field.Hidden,
                    field => field.TypeAsString)));

        ctx.ExecuteQuery();

        foreach (List oList in listInfo)
        {
            // Had to add these because trying to add in above query failed 
            ctx.Load(oList);
            ctx.ExecuteQuery();

            // 544 Base Template is MicroFeed
            if (oList.Hidden == false && oList.IsCatalog == false && (!oList.IsObjectPropertyInstantiated("IsSiteAssetsLibrary") || oList.IsSiteAssetsLibrary == false) && 
                oList.BaseType != BaseType.DocumentLibrary && oList.BaseTemplate != 544)
            {
                FieldCollection collField = oList.Fields;

                SharePointLibrary lib = new SharePointLibrary
                {
                    Title = oList.Title,
                    IsList = true,
                    Columns = new List<SharePointColumn>()
                };

                foreach (Microsoft.SharePoint.Client.Field oField in collField)
                {
                    if (!oField.Hidden)
                    {
                        SharePointColumn col = new SharePointColumn();
                        col.Title = oField.Title;
                        col.InternalName = oField.InternalName;
                        col.TypeAsString = oField.TypeAsString;
                        lib.Columns.Add(col);
                    }
                }

                sharePointLibraries.Add(lib);
            }
        }
    }