Create Sharepoint List items using Infopath 2010
To develop code using the SharePoint object model, you need to add a reference to Microsoft.SharePoint.dll to your VSTA project. This DLL is installed in %CommonProgramFiles%\Microsoft Shared\Web Server Extensions\14\ISAPI with your licensed copy of Microsoft SharePoint Server.
Below is the Code to add Category Name in the Category list in SharePoint.
public void Category_Changed(object sender, XmlEventArgs e)
{
using (SPSite FormSite = new SPSite(ServerInfo.SharePointSiteUrl.ToString()))
{
using (SPWeb FormWeb = FormSite.OpenWeb())
{
//Get the list
SPList CatList = FormWeb.Lists["Category"];
//I have changed the name of Title Column to Category in the list.
So, /my:myFields/my:Category is the xpath to the Category field and internal name would be "Title".
//Add an item to the Category list
SPListItem NewItem = CatList.Items.Add();
NewItem["Title"] = GetDomValue("/my:myFields/my:Category");
//Set AllowUnsafeUpdates = true to update the database
FormWeb.AllowUnsafeUpdates = true;
NewItem.Update();
FormWeb.AllowUnsafeUpdates = false;
} }}
private string GetDomValue(string XpathToGet)
{
return this.CreateNavigator().SelectSingleNode(XpathToGet, this.NamespaceManager).Value; }
Below is the Code to add Category Name in the Category list in SharePoint.
public void Category_Changed(object sender, XmlEventArgs e)
{
using (SPSite FormSite = new SPSite(ServerInfo.SharePointSiteUrl.ToString()))
{
using (SPWeb FormWeb = FormSite.OpenWeb())
{
//Get the list
SPList CatList = FormWeb.Lists["Category"];
//I have changed the name of Title Column to Category in the list.
So, /my:myFields/my:Category is the xpath to the Category field and internal name would be "Title".
//Add an item to the Category list
SPListItem NewItem = CatList.Items.Add();
NewItem["Title"] = GetDomValue("/my:myFields/my:Category");
//Set AllowUnsafeUpdates = true to update the database
FormWeb.AllowUnsafeUpdates = true;
NewItem.Update();
FormWeb.AllowUnsafeUpdates = false;
} }}
private string GetDomValue(string XpathToGet)
{
return this.CreateNavigator().SelectSingleNode(XpathToGet, this.NamespaceManager).Value; }
0 comments:
Post a Comment