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

时间:2009-08-26 09:35:22

标签: c# sharepoint content-type

我真的在寻找一个小代码片段,或者是关于这个主题的好教程。

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

提前致谢

5 个答案:

答案 0 :(得分:32)

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

http://blog.the-dargans.co.uk/2007/04/programmatically-adding-items-to.html http://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();
    

答案 1 :(得分:17)

简单来说,您需要按照步骤进行操作。

  1. 您需要将 Microsoft.SharePoint.dll 引用到该应用程序。
  2. 假设列表名称是测试,它只有一个字段“标题”,这里是代码。

            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. 您无需为自定义内容类型

  5. 创建自定义类

答案 2 :(得分: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()应该保存要枚举的列表项。

答案 3 :(得分:5)

这就是微软网站上的情况,我只是调整了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

答案 4 :(得分:0)

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

1-通过工具-> NuGet软件包管理器->管理NuGet软件包以解决方案->浏览->选择并安装

,添加Microsoft.SharePointOnline.CSOM

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("Username@domain.nz", 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)