Kae Travis

Insert into a SharePoint List using ASP.Net

Posted on by in SharePoint

This post describes how to insert into a SharePoint list using ASP.Net. First add a reference to the SharePoint DLL for your specific version. I’m working with SharePoint 2010, and the DLL can be found in the following location:

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\Microsoft.SharePoint.dll

Ensure in your page that you import the namespace:

using Microsoft.SharePoint;

Now I’ve just dumped some example code below, which inserts into a Text field (obtained from a Textbox), a Multi choice field (obtained from a CheckList box) and a User field (obtained from a SharePoint PeopleEditor control). I’ve also left a few comments in intentionally, as I tried different ways of doing things. I found that if I ran this chunk of code with elevated privileges it inserted the entry into the list but didn’t trigger the associated workflow. I also tried to run it as an impersonated user, but it still didn’t trigger the workflow. So I commented out the RunWithElevated privileges line and it all worked well – the list item inserted and the associated workflow triggered.

You can also see that for the list name, I store this in the Web.Config file since I use the same list throughout the site.

try
{
//we can use this to run in the context of a specific user, by passing in
//superToken to SPSite
// var user = SPContext.Current.Web.AllUsers[@"domain\user"];
//var superToken = user.UserToken;
//**IMPORTANT - Cannot run elevated otherwise Workflow wont run!!**
// SPSecurity.RunWithElevatedPrivileges(delegate
//{
using (SPSite oSite = new SPSite(ConfigurationManager.AppSettings["ApplicationPortal"].ToString()))
{
using (SPWeb oweb = oSite.OpenWeb())
{
oSite.AllowUnsafeUpdates = true;
oweb.AllowUnsafeUpdates = true;
SPList supportLib = oweb.Lists[ConfigurationManager.AppSettings["UAMRequestList"].ToString()];
SPListItem newItem = supportLib.Items.Add();
//add a string to a Text field
newItem["Title"] = "My Title";
//add to a multi choice field
SPFieldMultiChoiceValue tp = new SPFieldMultiChoiceValue();
//loop through each item in the checkbox list.  If checked, add it
foreach (ListItem cBox in targetplatformCBL.Items)
{
if (cBox.Selected)
{
tp.Add(cBox.Text);                          
}
}                  
newItem["Target_x0020_Platform"] = tp;
//add to a User field
// SPUser user = oweb.EnsureUser("domain\\user");
//string sRequester = user.ID.ToString() + ";#" + user.LoginName.ToString();
// newItem["Requester"] = sRequester;
PickerEntity entPropMgr = (PickerEntity)requesterPE.ResolvedEntities[0];
SPFieldUserValue Proposalmgrvalue = new SPFieldUserValue(SPContext.Current.Web, Convert.ToInt16(entPropMgr.EntityData[PeopleEditorEntityDataKeys.UserId]), entPropMgr.Description);
newItem["Requester"] = Proposalmgrvalue;
newItem.Update();
}
}
}
catch (SPException ex)
{
//handle exception   
}

 

Insert into a SharePoint List using ASP.Net
Insert into a SharePoint List using ASP.Net

Leave a Reply