This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
using System.Drawing; | |
using System.Linq; | |
using System.Text; | |
using System.Windows.Forms; | |
using Microsoft.SharePoint; | |
namespace WindowsFormsApplication1 | |
{ | |
public partial class Form1 : Form | |
{ | |
public Form1() | |
{ | |
InitializeComponent(); | |
} | |
SPSite siteCollection; | |
SPWeb site; | |
private void btnCreateSite_Click(object sender, EventArgs e) | |
{ | |
try | |
{ | |
siteCollection = new SPSite(txtSiteCollection.Text); | |
site = siteCollection.OpenWeb(); | |
SPListCollection listCollection = site.Lists; | |
listCollection.Add(txtListName.Text, "list description", SPListTemplateType.GenericList); | |
} | |
catch (Exception) | |
{ } | |
finally | |
{ | |
site.Dispose(); | |
siteCollection.Close(); | |
} | |
} | |
} | |
} | |
#region Sample | |
//Adding a List Item to a Custom List | |
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 List | |
public static SPListItem OptimizedAddItem(SPList list) | |
{ | |
const string EmptyQuery = "0"; | |
SPQuery q = new SPQuery {Query = EmptyQuery}; | |
return list.GetItems(q).Add(); | |
} | |
//-------------------------------------------------- | |
//Adding a List Item to a Custom List with an Attachment | |
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 List in SharePoint 2010 | |
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(); | |
} | |
//--------------------------------------------- | |
#endregion | |