【问题标题】:SharePoint : How can I programmatically add items to a custom list instanceSharePoint:如何以编程方式将项目添加到自定义列表实例
【发布时间】:2010-11-22 22:33:37
【问题描述】:

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

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

提前致谢

【问题讨论】:

    标签: c# sharepoint content-type


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 您能否详细说明“您需要在同一服务器上运行此应用程序”?如果我使用 SharePoint.dll 在一台服务器上创建此应用程序并为其他 SharePoint 站点运行它会怎样。
      【解决方案3】:

      您可以在自定义 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() 应该保存正在枚举的列表项。

      【讨论】:

      • list.Additem() 据我了解,适用于 2007 年更高的版本。 2007 年 - 只有 list.Items.Add(),(从性能角度来看,这很难看)
      【解决方案4】:

      这就是 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

      【讨论】:

        【解决方案5】:

        我遇到了类似的问题,并且能够按照以下方法解决它(类似于其他答案,但也需要凭据),

        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("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)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-01-09
          • 2015-04-01
          • 2012-04-09
          相关资源
          最近更新 更多