MOSS 2007 - Create List Item using aspx query string - SharePoint Development & Administration + InfoPath

Tuesday, April 28, 2009

MOSS 2007 - Create List Item using aspx query string

Can’t get email enabled document libraries working?

I was unable to get email enabled libraries to work in my SharePoint environment due to security issues relating to Active Directory account creation. The following code takes the query string parameters and creates a list item with the metadata in the specified list. I have this script being called by event handlers in Outlook, which call the page / script, passing the required details in the query string.

By default, SharePoint (MOSS 2007) has code block enabled. This means that a page or script won’t load / run if there it contains code. To disable code blocks in MOSS, please see my post
Code Blocks in MOSS 2007 for instructions on how to disable code blocks.

The following code must be placed inside the aspx page the you would like to create the list items via the query string:

<script type="text/c#" runat="server">

/*---------------------------------------------------------------------
This page/script is used to create a list item using a GET query
string.
---------------------------------------------------------------------*/

//Declare Variables
public String siteURL;
public String list;
public String itemSubject;
public String itemSentBy;
public String itemSentTo;
public String itemSentOn;
public String linkToItem;
public String received;
public String strCc;
public String strBcc;
public String itemSent;

protected void Page_Load(object sender, EventArgs e)
{

/*---------------------------------------------------------------------
This function runs when the page loads, which calls all required
functions to create the list item. This page does not Display
any content.
---------------------------------------------------------------------*/


//form1.InnerHtml = "<h1>Hello</h1>";
//AddNewListItem();
getQueryStringValues();

InsertEnquiryToSharepoint();
}


/*---------------------------------------------------------------------
This function stores the query string values into public variables
---------------------------------------------------------------------*/
public void getQueryStringValues()
{

siteURL = Request.QueryString["URL"];
list = Request.QueryString["List"];
itemSubject = Request.QueryString["Subject"];
itemSentBy =
Request.QueryString["SentBy"];
itemSentTo = Request.QueryString["SentTo"];
itemSentOn = Request.QueryString["DateSent"];
received = Request.QueryString["Received"];
strCc = Request.QueryString["Cc"];
strBcc = Request.QueryString["Bcc"];
itemSent = Request.QueryString["ItemSent"];
}


/*---------------------------------------------------------------------
This function opens the required SharePoint Site and list and
creates a list item with the values supplied via the query string
---------------------------------------------------------------------*/
protected void InsertEnquiryToSharepoint()
{

string strDashListRoot = "http://sitecollectionroot/Lists/EmailRegister/";
using(SPSite site = new SPSite(strDashListRoot))
{

using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;

SPList list = web.Lists["List Name"];
SPListItem item = list.Items.Add();

item["Subject"] = itemSubject;
item["To"] = itemSentTo;
item["From"] = itemSentBy;

item["Sent"] = itemSentOn;
item["Received"] = received;
item["Cc"] = strCc;
item["Bcc"] = strBcc;

item.Update();
form1.InnerHtml = "<p>Item Created
Successfully:<br/>Site Name: " + web.Title +
"</p><br/>List Name: " + list.Title;
}
}
}

</script>

Share this article:
Stumble This Delicious
Delicious
submit to reddit
Facebook
MySpace
MySpace

No comments:

Post a Comment