尝试使用Visual Studio 2017将现有网站列添加到自定义列表| (Sharepoint)[Microsoft.Sharepoint.Client]

时间:2019-07-05 10:29:15

标签: c# visual-studio sharepoint visual-studio-2017 sharepoint-online

我正在构建一个代码,在其中将一些数据在线插入到SharePoint中,但是无法识别自定义列区域中的“产品”项。 我想构建一个将现有站点列(自定义列)添加到“产品列表”(主列表)的代码。

目前唯一要做的是将现有的自定义列手动添加到我的自定义列表中。

        List lst = web.Lists.GetByTitle("Product List");
        ListItemCreationInformation itmCreationInfo = new ListItemCreationInformation();
        ListItem newItem = lst.AddItem(itmCreationInfo);
        newItem["Title"] = "This is a title! Yey!";
        newItem["Product"] = "Ultimate Gaming PC with gaming console";
        newItem.Update();
        ctx.ExecuteQuery();
        Console.WriteLine("All tasks completed.  Press any key to close...");
        Console.ReadLine();

我希望输出类似于“产品”已自动添加到主列表的列中。

1 个答案:

答案 0 :(得分:0)

请参考以下代码,将网站栏自动添加到列表中:

        string userName = "user@tenant.onmicrosoft.com";
        string password = "***********";
        var securePassword = new SecureString();
        foreach (char c in password)
        {
            securePassword.AppendChar(c);
        }
        using (var clientContext = new ClientContext("https://tenant.sharepoint.com/sites/sitename"))
        {
            clientContext.Credentials = new SharePointOnlineCredentials(userName, securePassword);
            Web web = clientContext.Web;
            clientContext.Load(web);
            clientContext.ExecuteQuery();
            List targetList = clientContext.Web.Lists.GetByTitle("Product List");
            clientContext.Load(targetList);
            clientContext.ExecuteQuery();
            string siteCols = "Product";
            FieldCollection fieldCollection = clientContext.Web.AvailableFields;
            clientContext.Load(fieldCollection);
            clientContext.ExecuteQuery();
            Field myField = Enumerable.FirstOrDefault(fieldCollection, ft => ft.InternalName == siteCols);
            targetList.Fields.Add(myField);
            targetList.Update();
            clientContext.ExecuteQuery();
        }   

enter image description here