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

后端 未结 5 1347
独厮守ぢ
独厮守ぢ 2021-02-01 17:22

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 l

5条回答
  •  长发绾君心
    2021-02-01 18:14

    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:

    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.

提交回复
热议问题