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.

Monday, November 28, 2011

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(""); writer.Write(""); writer.Write(""); writer.Write(""); writer.Write(""); writer.Write(""); writer.Write(""); writer.Write(""); writer.Write(""); writer.Write(""); writer.Write(""); writer.Write(""); writer.Write(""); writer.Write(""); writer.Write(""); writer.Write(""); writer.Write(""); writer.Write(""); writer.Write(""); writer.Write(""); writer.Write(""); writer.Write(""); writer.Write("
");
writer.Write("Select Project");
writer.Write("
");
ddlSubsite.RenderControl(writer);
writer.Write("
");
writer.Write("Select List");
writer.Write("
");
ddlLists.RenderControl(writer);
writer.Write("
");
writer.Write("Enter Title");
writer.Write("
");
txtTitle.RenderControl(writer);
writer.Write("
");
btnSave.RenderControl(writer);
writer.Write(" ");
btnCancel.RenderControl(writer);
writer.Write("
");
}
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