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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1333492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 15:15:42  来源:igfitidea点击:

SharePoint : How can I programmatically add items to a custom list instance

c#sharepointcontent-type

提问by JL.

I am really looking for either a small code snippet, or a good tutorial on the subject.

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

I have a C# console app that I will use to somehow add list items to my custom list. I have created a custom content type too. So not sure if I need to create an C# class from this content type too. Perhaps not.

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

Thanks in advance

提前致谢

采纳答案by Flo

I think these both blog post should help you solving your problem.

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

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/

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/

Short walk through:

短途游:

  1. Get a instance of the list you want to add the item to.
  2. Add a new item to the list:

    SPListItem newItem = list.AddItem();
    
  3. To bind you new item to a content type you have to set the content type id for the new item:

    newItem["ContentTypeId"] = <Id of the content type>;
    
  4. Set the fields specified within your content type.

  5. Commit your changes:

    newItem.Update();
    
  1. 获取要将项目添加到的列表实例。
  2. 向列表中添加一个新项目:

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

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

  5. 提交您的更改:

    newItem.Update();
    

回答by Kusek

To put it simple you will need to follow the step.

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

  1. You need to reference the Microsoft.SharePoint.dllto the application.
  2. Assuming the List Name is Testand it has only one Field "Title" here is the code.

            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. Note that you need to run this application in the Same server where the SharePoint is installed.

  4. You dont need to create a Custom Class for Custom Content Type

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

            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. 您不需要为自定义内容类型创建自定义类

回答by Andrew

You can create an item in your custom SharePoint list doing something like this:

您可以在自定义 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();
     }
}

Using list.AddItem() should save the lists items being enumerated.

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

回答by vapcguy

This is how it was on the Microsoft site, with me just tweaking the SPSite and SPWeb since these might vary from environment to environment and it helps not to have to hard-code these:

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

Source: SPListItemClass (Microsoft.SharePoint). (2012). Retrieved February 22, 2012, from http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splistitem.aspx.

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