|
The below code shows how to create a List Item programmatically using the object model in SharePoint 2007. Adding a List Item to a Custom ListUnable to find source-code formatter for language: csharp. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml using (SPWeb web = siteCollection.AllWebs["webname"]) { SPList list = web.Lists["Custom List"]; SPListItem item = list.Items.Add(); item["Title"] = "New List Item"; item.Update(); } Optimised Adding a List Item to a Custom ListUse the following to add an item to a list: Unable to find source-code formatter for language: csharp. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml 01: public static SPListItem OptimizedAddItem(SPList list) 02: { 03: const string EmptyQuery = "0"; 04: SPQuery q = new SPQuery {Query = EmptyQuery}; 05: return list.GetItems(q).Add(); 06: } Do not use SPList.Items.Add as this will get all items in the list before adding a new SPListItem. Source: Aidan Garnish Adding a List Item to a Custom List with an AttachmentUnable to find source-code formatter for language: csharp. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml using (SPWeb web = siteCollection.AllWebs["webname"]) { SPList list = web.Lists["Custom List"]; SPListItem item = list.Items.Add(); item["Title"] = "New List Item"; SPAttachmentCollection attachments = item.Attachments; attachments.Add(fileName, byteArrayContents); item.Update(); } Adding a List Item to a Publishing Page Libraryusing (SPSite site = new SPSite("http://moss")) { using (SPWeb web = site.OpenWeb()) { PublishingSite pSite = new PublishingSite(site); SPContentType ctype = pSite.ContentTypes["Welcome Page"]; PageLayoutCollection pageLayouts = pSite.GetPageLayouts(ctype, true); PageLayout pageLayout = pageLayouts.FirstOrDefault<PageLayout>(p => p.Name == "WelcomeSplash.aspx"); PublishingWeb pWeb = PublishingWeb.GetPublishingWeb(web); PublishingPageCollection pPages = pWeb.GetPublishingPages(); PublishingPage pPage = pPages.Add("Programmatic_Test.aspx", pageLayout); SPListItem newpage = pPage.ListItem; newpage["Title"] = "Page added programmatically"; newpage.Update(); newpage.File.CheckIn("all looks good"); newpage.File.Publish("all looks good"); } } NOTE: requires .NET 3.5 for System.Linq FirstOrDefault method, can be switched for a loop. Adding a List Item to a Document Library with an attachmentSource: Nick Grattan's SharePoint Blog Unable to find source-code formatter for language: csharp. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml // Creates document in given list (root folder). // Returns true if the file was created, false if it already // exists or throws exception for other failure protected bool CreateDocument( string sFilename, string sContentType, string sList) { try { SPSite site = SPContext.Current.Site; using (SPWeb web = site.OpenWeb()) { SPList list = web.Lists[sList]; // this always uses root folder SPFolder folder = web.Folders[sList]; SPFileCollection fcol = folder.Files; // find the template url and open string sTemplate = list.ContentTypes[sContentType].DocumentTemplateUrl; SPFile spf = web.GetFile(sTemplate); byte[] binFile = spf.OpenBinary(); // Url for file to be created string destFile = fcol.Folder.Url + "/" + sFilename; // create the document and get SPFile/SPItem for // new document SPFile addedFile = fcol.Add(destFile, binFile, false); SPItem newItem = addedFile.Item; newItem["ContentType"] = sContentType; newItem.Update(); addedFile.Update(); return true; } } catch (SPException spEx) { // file already exists? if (spEx.ErrorCode == -2130575257) return false; else throw spEx; } } This code: 1. Gets a SPSite for the current site collection, and opens an SPWeb for the site. Adding a List Item to a Calendar ListSource: Andy - novolocus.com Unable to find source-code formatter for language: csharp. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml SPList cal = site.Lists["Calendar"]; SPListItem calEvent = cal.Items.Add(); calEvent["Title"] = "Frequent Event"; string recurrence = "<recurrence><rule>" + "<firstDayOfWeek>su</firstDayOfWeek>" + "<repeat><daily dayFrequency='2?/></repeat>" + "<windowEnd>2010-09-20T09:00:00Z</windowEnd>" + "</rule></recurrence>"; calEvent["RecurrenceData"] = recurrence; calEvent["EventType"] = 1; calEvent["EventDate"] = new DateTime(2009, 1, 26, 8, 0, 0); calEvent["EndDate"] = new DateTime(2009, 1, 26, 9, 0, 0); calEvent["UID"] = System.Guid.NewGuid(); calEvent["TimeZone"] = 13; calEvent["Recurrence"] = -1; Note that the CAML to create the recurrence can be complicated so please take a look at the MSDN article Adding a List Item to a List in SharePoint 2010In SharePoint 2010 you can use the following code to add an item to a SPList in an optimized way: Unable to find source-code formatter for language: csharp. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml using (SPWeb web = SPContext.Current.Web)
{
SPList list = web.GetList(string.concat(web.Url, "/Lists/Custom List"));
SPListItem item = list.AddItem();
item["Title"] = "New List Item";
item.Update();
}
By Koen Zomers Labels |
Creating a List Item instance programmatically using the object model

This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License. Hosted generously by CustomWare









Comments (4)
May 18, 2009
Anonymous says:
Could you tell me adding a List Item to a Document Library w/o harcoding t...Could you tell me adding a List Item to a Document Library w/o harcoding the folder name [Programatically].
Sep 18, 2009
Anonymous says:
great stuff but your adding a List Item to a Document Library with an attachment...great stuff but your adding a List Item to a Document Library with an attachment has an error once your have generated a word 2007 document, when trying to open the word document from the doc lib it says the office openxml document failed to open because it has no contents or something I even noticed that the document has no pages inside I have been dying trying to get a working solution that will generate a new word 2007 document based on a doc lib template.dotx file and populate the file with text and saving it back into the lib
Jun 05, 2009
Anonymous says:
Nice wrok.Nice wrok.
Dec 08, 2009
Anonymous says:
NIce work. The document creation code is really very useful. ThanksNIce work. The document creation code is really very useful. Thanks