SharePoint:如何以编程方式将项目添加到自定义列表实例中

问题描述 投票:24回答:5

我真的在寻找有关此主题的小代码段或优秀教程。

我有一个C#控制台应用程序,将以某种方式将列表项添加到自定义列表中。我也创建了一个自定义内容类型。因此,不确定是否也需要从此内容类型创建C#类。也许不是。

提前感谢

c# sharepoint content-type
5个回答
33
投票

我认为这两篇博文都应该帮助您解决问题。

http://blog.the-dargans.co.uk/2007/04/programmatically-adding-items-to.htmlhttp://asadewa.wordpress.com/2007/11/19/adding-a-custom-content-type-specific-item-on-a-sharepoint-list/

短时间浏览:

  1. 获取要向其中添加项目的列表的实例。
  2. 向列表添加新项目:

    SPListItem newItem = list.AddItem();
    
  3. 要将新项目绑定到内容类型,您必须设置新项目的内容类型ID:

    newItem["ContentTypeId"] = <Id of the content type>;
    
  4. 设置您的内容类型中指定的字段。

  5. 提交您的更改:

    newItem.Update();
    

18
投票

简单起见,您需要执行以下步骤。

  1. 您需要引用Microsoft.SharePoint.dll到应用程序。
  2. 假设列表名称为Test,并且只有一个字段“ Title”,这里是代码。

            using (SPSite oSite=new SPSite("http://mysharepoint"))
        {
            using (SPWeb oWeb=oSite.RootWeb)
            {
                SPList oList = oWeb.Lists["Test"];
                SPListItem oSPListItem = oList.Items.Add();
                oSPListItem["Title"] = "Hello SharePoint";
                oSPListItem.Update();
            }
    
        }
    
  3. 请注意,您需要在安装SharePoint的同一服务器上运行此应用程序。

  4. 您不需要为自定义内容类型创建自定义类


11
投票

您可以在自定义SharePoint列表中创建一个执行以下操作的项目:

using (SPSite site = new SPSite("http://sharepoint"))
{
    using (SPWeb web = site.RootWeb)
    {
        SPList list = web.Lists["My List"];
        SPListItem listItem = list.AddItem();
        listItem["Title"] = "The Title";
        listItem["CustomColumn"] = "I am custom";
        listItem.Update();
     }
}

使用list.AddItem()应该保存要枚举的列表项。


5
投票

这就是Microsoft网站上的情况,我只是对SPSite和SPWeb进行了调整,因为它们可能因环境而异,因此不必对它们进行硬编码:

using (SPSite oSiteCollection = new SPSite(SPContext.Current.Site.Url))
{
    using (SPWeb oWeb = oSiteCollection.OpenWeb(SPContext.Current.Web))
    {
        SPList oList = oWeb.Lists["Announcements"];
        // You may also use 
        // SPList oList = oWeb.GetList("/Lists/Announcements");
        // to avoid querying all of the sites' lists
        SPListItem oListItem = oList.Items.Add();
        oListItem["Title"] = "My Item";
        oListItem["Created"] = new DateTime(2004, 1, 23);
        oListItem["Modified"] = new DateTime(2005, 10, 1);
        oListItem["Author"] = 3;
        oListItem["Editor"] = 3;
        oListItem.Update();
    }
}

来源:SPListItemClass(Microsoft.SharePoint)。 (2012)。于2012年2月22日从http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splistitem.aspx中检索。


0
投票

我有一个类似的问题,可以通过以下方法解决(类似于其他答案,但也需要凭据),

1-通过工具添加Microsoft.SharePointOnline.CSOM-> NuGet软件包管理器->管理解决方案的NuGet软件包->浏览->选择并安装

2-添加“使用Microsoft.SharePoint.Client;”

然后输入下面的代码

        string siteUrl = "https://yourcompany.sharepoint.com/sites/Yoursite";
        SecureString passWord = new SecureString();

        var password = "Your password here";
        var securePassword = new SecureString();
        foreach (char c in password)
        {
            securePassword.AppendChar(c);
        }
        ClientContext clientContext = new ClientContext(siteUrl);
        clientContext.Credentials = new SharePointOnlineCredentials("[email protected]", securePassword);/*passWord*/
        List oList = clientContext.Web.Lists.GetByTitle("The name of your list here");
        ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
        ListItem oListItem = oList.AddItem(itemCreateInfo);
        oListItem["PK"] = "1";
        oListItem["Precinct"] = "Mangere";
        oListItem["Title"] = "Innovation";
        oListItem["Project_x0020_Name"] = "test from C#";
        oListItem["Project_x0020_ID"] = "ID_123_from C#";
        oListItem["Project_x0020_start_x0020_date"] = "2020-05-01 01:01:01";
        oListItem.Update();

        clientContext.ExecuteQuery();

[请记住,您的字段可能与您看到的字段不同,例如,在我的列表中,我看到的是“项目名称”,而实际值为“ Project_x0020_ID”。如何获取这些值(即内部字段值)?

一些方法:

1-使用MS流并查看它们

2- https://mstechtalk.com/check-column-internal-name-sharepoint-list/https://sharepoint.stackexchange.com/questions/787/finding-the-internal-name-and-display-name-for-a-list-column

3-使用C#阅读器并阅读您的共享点列表

其余操作(更新/删除):https://docs.microsoft.com/en-us/previous-versions/office/developer/sharepoint-2010/ee539976(v%3Doffice.14)

© www.soinside.com 2019 - 2024. All rights reserved.