Tuesday, January 3, 2012

Inserting Data into the subsite lists dynamically.


using System;
using System.Runtime.InteropServices;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Security;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using System.Collections.Specialized;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Configuration;
using System.Data;
[assembly: AllowPartiallyTrustedCallers]
namespace TestSharePointDll1
{
public class TestSharePointDll1 : System.Web.UI.WebControls.WebParts.WebPart
{
string strError = string.Empty;
DropDownList ddlSubsite;
DropDownList ddlLists;
TextBox txtTitle;
Button btnSave;
Button btnCancel;
protected override void Render(HtmlTextWriter writer)
{
writer.Write(strError);
try
{
writer.Write("<Table width='100%'>");
writer.Write("<Tr>");
writer.Write("<Td>");
writer.Write("Select Project");
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td Align='Right'>");
ddlSubsite.RenderControl(writer);
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td>");
writer.Write("Select List");
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td Align='Right'>");
ddlLists.RenderControl(writer);
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td>");
writer.Write("Enter Title");
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td Align='Right'>");
txtTitle.RenderControl(writer);
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td colspan='2' Align='Center'>");
btnSave.RenderControl(writer);
writer.Write("&nbsp;");
btnCancel.RenderControl(writer);
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("</Table>");
}
catch (Exception ex)
{
writer.Write(ex.ToString());
}
}
protected override void CreateChildControls()
{
try
{
SPWeb currentWeb = SPControl.GetContextWeb(Context);
ddlSubsite = new DropDownList();
ddlSubsite.ID = "ddlSubsite";
ddlSubsite.AutoPostBack = true;
foreach (SPWeb subWeb in currentWeb.GetSubwebsForCurrentUser())
{
ListItem li = new ListItem();
li.Value = subWeb.Title;
li.Text = subWeb.ID.ToString();
ddlSubsite.Items.Add(li);
}
ddlSubsite.SelectedIndexChanged += new EventHandler(ddlSubsite_SelectedIndexChanged);
this.Controls.Add(ddlSubsite);
currentWeb.Dispose();
ddlLists = new DropDownList();
ddlLists.ID = "ddlLists";
this.Controls.Add(ddlLists);
txtTitle = new TextBox();
txtTitle.ID = "txtTitle";
this.Controls.Add(txtTitle);
btnSave = new Button();
btnSave.ID = "btnSave";
btnSave.Text = "Save Record";
btnSave.Click += new EventHandler(btnSave_Click);
this.Controls.Add(btnSave);
btnCancel = new Button();
btnCancel.ID = "btnCancel";
btnCancel.CausesValidation = false;
btnCancel.Click += new EventHandler(btnCancel_Click);
this.Controls.Add(btnCancel);
}
catch (Exception ex)
{
strError += ex.ToString();
}
}
void btnCancel_Click(object sender, EventArgs e)
{
try
{
SPWeb currentWeb = SPControl.GetContextWeb(Context);
Page.Response.Redirect(currentWeb.Url +"/Prasad Data/Test.aspx");
}
catch (Exception ex)
{
strError += ex.ToString();
}
}
void ddlSubsite_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
SPWeb currentWeb1 = SPControl.GetContextWeb(Context);
foreach (SPWeb subWeb1 in currentWeb1.GetSubwebsForCurrentUser())
{
if (subWeb1.Title == ddlSubsite.SelectedItem.Text)
{
foreach (SPList lstSubWebLists in subWeb1.Lists)
{
ListItem liLists = new ListItem();
liLists.Text = lstSubWebLists.Title;
liLists.Value = lstSubWebLists.ID.ToString();
ddlLists.Items.Add(liLists);
}
}
}
currentWeb1.Dispose();
}
catch (Exception ex)
{
strError += ex.ToString();
}
}
void btnSave_Click(object sender, EventArgs e)
{
try
{
SPWeb currentWeb = SPControl.GetContextWeb(Context);
foreach (SPWeb objweb in currentWeb.GetSubwebsForCurrentUser())
{
if (objweb.Title == ddlSubsite.SelectedItem.Text)
{
SPList lstObject = objweb.Lists[ddlLists.SelectedItem.Text];
{
SPListItemCollection mycoll = lstObject.Items;
SPListItem item = mycoll.Add();
item["Title"] = txtTitle.Text;
strError += "Record Saved Successfully";
break;
}
}
}
currentWeb.Dispose();
}
catch (Exception ex)
{
strError += ex.ToString();
}
}
}
}

No comments:

Post a Comment