繁体   English   中英

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

[英]Trying to add existing site column to the custom list using Visual Studio 2017 | (Sharepoint) [Microsoft.Sharepoint.Client]

我正在构建一个代码,在其中将一些数据插入到在线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();

我希望输出像“产品”一样已自动添加到主列表的列中。

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

        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();
        }   

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM