Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Thursday, December 01, 2011

Sample Code to write a batch using SpWeb Object



using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Security;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
[assembly:AllowPartiallyTrustedCallers]
namespace TestSampleRD
{
[Guid("d8484a38-f485-4eff-8532-7d5e5cf98a45")]
public class TestSampleRD : System.Web.UI.WebControls.WebParts.WebPart
{
TextBox txtUserName;
TextBox txtPassWord;
Button btnSave;
string strError = string.Empty;
public TestSampleRD()
{
}
protected override void Render(HtmlTextWriter writer)
{
try
{
writer.Write("<Table>");
writer.Write("<Tr>");
writer.Write("<Td Colspan='2' Align='Center'>");
writer.Write("Batch and SpWeb Sample...!");
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td>");
writer.Write("Enter UserName");
writer.Write("</Td>");
writer.Write("<Td>");
txtUserName.RenderControl(writer);
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td>");
writer.Write("Enter PassWord");
writer.Write("</Td>");
writer.Write("<Td>");
txtPassWord.RenderControl(writer);
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td Colspan='2' Align='Center'>");
btnSave.RenderControl(writer);
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("</Table>");
writer.Write(strError);
}
catch (Exception ex)
{
writer.Write(ex.ToString());
}
}
protected override void CreateChildControls()
{
try
{
txtUserName = new TextBox();
txtUserName.ID = "txtUserName";
this.Controls.Add(txtUserName);
txtPassWord = new TextBox();
txtPassWord.ID = "txtPassWord";
this.Controls.Add(txtPassWord);
btnSave = new Button();
btnSave.ID = "btnSave";
btnSave.Text = "Save Record";
btnSave.Click+=new EventHandler(btnSave_Click);
this.Controls.Add(btnSave);
}
catch (Exception ex)
{
strError += ex.ToString();
}
}
void btnSave_Click(object sender, EventArgs e)
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWeb currentWeb = SPControl.GetContextWeb(Context);
using (SPSite site = new SPSite(currentWeb.Url))
{
using (SPWeb objWeb = site.OpenWeb())
{
objWeb.AllowUnsafeUpdates = true;
SPList lst = objWeb.Lists["Login_Dat001"];
System.Guid Guid = lst.ID;
string strBatch = string.Empty;
strBatch += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<ows:Batch OnError=\"Return\">" +
"<Method ID=\"1\"><SetList>" + Guid + "</SetList>" +
"<SetVar Name=\"ID\">New</SetVar>" +
"<SetVar Name=\"Cmd\">Save</SetVar>" +
"<SetVar Name='urn:schemas-microsoft-com:office:office#UserName'>" + txtUserName.Text + "</SetVar><SetVar Name='urn:schemas-microsoft-com:office:office#PassWord'>" + txtPassWord.Text + "</SetVar></Method></ows:Batch>";
string str=objWeb.ProcessBatchData(strBatch);
strError += "Insertion Successful";
txtUserName.Text = "";
txtPassWord.Text = "";
}
}
});
}
catch (Exception ex)
{
strError += ex.ToString();
}
}
}
}

No comments:

Post a Comment