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;
using System.Data;
[assembly:AllowPartiallyTrustedCallers]
namespace TestSampleRD
{
[Guid("d8484a38-f485-4eff-8532-7d5e5cf98a45")]
public class TestSampleRD : System.Web.UI.WebControls.WebParts.WebPart
{
TextBox txtTaskName;
PeopleEditor peAssignedTo;
DropDownList ddlStatus;
DropDownList ddlPriority;
DateTimeControl dtStartDate;
DateTimeControl dtDueDate;
Button btnCreateTask;
string strError = string.Empty;
public TestSampleRD()
{
}
protected override void Render(HtmlTextWriter writer)
{
try
{
writer.Write("<Table width='100%'>");
writer.Write("<Tr>");
writer.Write("<Td>");
writer.Write("Enter Task Name");
writer.Write("</Td>");
writer.Write("<Td>");
txtTaskName.RenderControl(writer);
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td>");
writer.Write("Enter Assigned To");
writer.Write("</Td>");
writer.Write("<Td>");
peAssignedTo.RenderControl(writer);
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td>");
writer.Write("Select Status");
writer.Write("</Td>");
writer.Write("<Td>");
ddlStatus.RenderControl(writer);
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td>");
writer.Write("Select Priority");
writer.Write("</Td>");
writer.Write("<Td>");
ddlPriority.RenderControl(writer);
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td>");
writer.Write("Select Start Date");
writer.Write("</Td>");
writer.Write("<Td>");
dtStartDate.RenderControl(writer);
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td>");
writer.Write("Select Due Date");
writer.Write("</Td>");
writer.Write("<Td>");
dtDueDate.RenderControl(writer);
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td Colspan='2' Align='Center'>");
btnCreateTask.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
{
txtTaskName = new TextBox();
txtTaskName.ID = "txtTaskName";
this.Controls.Add(txtTaskName);
peAssignedTo = new PeopleEditor();
peAssignedTo.ID = "peAssignedTo";
peAssignedTo.MultiSelect = false;
this.Controls.Add(peAssignedTo);
ddlStatus = new DropDownList();
ddlStatus.ID = "ddlStatus";
SPWeb currentWeb = SPControl.GetContextWeb(Context);
SPQuery sQuery = new SPQuery();
sQuery.Query = "<OrderBy><FieldRef Name='ID'
Ascending='True' /></OrderBy>";
SPList lstStatus = currentWeb.Lists["Status"];
SPListItemCollection myColl_Status =
lstStatus.GetItems(sQuery);
if (myColl_Status.Count > 0)
{
ddlStatus.DataValueField = "ID";
ddlStatus.DataTextField = "Title";
ddlStatus.DataSource = myColl_Status.GetDataTable();
ddlStatus.DataBind();
}
ddlStatus.Items.Insert(0, new ListItem("Select Status"));
this.Controls.Add(ddlStatus);
ddlPriority = new DropDownList();
ddlPriority.ID = "ddlPriority";
SPList lstPriority = currentWeb.Lists["Priority"];
SPListItemCollection myColl_Priority =
lstPriority.GetItems(sQuery);
if (myColl_Priority.Count > 0)
{
ddlPriority.DataValueField = "ID";
ddlPriority.DataTextField = "Title";
ddlPriority.DataSource = myColl_Priority.GetDataTable();
ddlPriority.DataBind();
}
ddlPriority.Items.Insert(0, new ListItem("Select
Priority"));
this.Controls.Add(ddlPriority);
dtStartDate = new DateTimeControl();
dtStartDate.ID = "dtStartDate";
this.Controls.Add(dtStartDate);
dtDueDate = new DateTimeControl();
dtDueDate.ID = "dtDueDate";
this.Controls.Add(dtDueDate);
btnCreateTask = new Button();
btnCreateTask.ID = "btnCreateTask";
btnCreateTask.Text = "Create Task";
btnCreateTask.Click += new
EventHandler(btnCreateTask_Click);
this.Controls.Add(btnCreateTask);
}
catch (Exception ex)
{
strError += ex.ToString();
}
}
void btnCreateTask_Click(object sender, EventArgs e)
{
try
{
SPWeb curentWeb = SPControl.GetContextWeb(Context);
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(curentWeb.Url))
{
using (SPWeb objWeb = site.OpenWeb())
{
objWeb.AllowUnsafeUpdates = true;
SPList lstTasks = curentWeb.Lists["Sample
Tasks"];
SPListItemCollection myColl = lstTasks.Items;
SPListItem item = myColl.Add();
item["Title"] = txtTaskName.Text;
item["AssignedTo"] =
objWeb.AllUsers[peAssignedTo.Accounts[0].ToString()];
if (ddlStatus.SelectedIndex != 0)
{
item["Status"] =
ddlStatus.SelectedValue.ToString()+";#"+ddlStatus.SelectedItem.Text;
}
if (ddlPriority.SelectedIndex !=0)
{
item["Priority"] =
ddlPriority.SelectedValue.ToString() + ";#"+ddlPriority.SelectedItem.Text;
}
item["StartDate"] =
Convert.ToDateTime(dtStartDate.SelectedDate.ToShortDateString());
item["DueDate"] =
Convert.ToDateTime(dtDueDate.SelectedDate.ToShortDateString());
item.Update();
strError += "Task Successfully Created...!";
Page.Response.Redirect(curentWeb.Url
+"/Lists/Sample Tasks/AllItems.aspx");
}
}
});
}
catch (Exception ex)
{
strError += ex.ToString();
}
}
}
}
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;
using System.Data;
[assembly:AllowPartiallyTrustedCallers]
namespace TestSampleRD
{
[Guid("d8484a38-f485-4eff-8532-7d5e5cf98a45")]
public class TestSampleRD : System.Web.UI.WebControls.WebParts.WebPart
{
TextBox txtTaskName;
PeopleEditor peAssignedTo;
DropDownList ddlStatus;
DropDownList ddlPriority;
DateTimeControl dtStartDate;
DateTimeControl dtDueDate;
Button btnCreateTask;
string strError = string.Empty;
public TestSampleRD()
{
}
protected override void Render(HtmlTextWriter writer)
{
try
{
writer.Write("<Table width='100%'>");
writer.Write("<Tr>");
writer.Write("<Td>");
writer.Write("Enter Task Name");
writer.Write("</Td>");
writer.Write("<Td>");
txtTaskName.RenderControl(writer);
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td>");
writer.Write("Enter Assigned To");
writer.Write("</Td>");
writer.Write("<Td>");
peAssignedTo.RenderControl(writer);
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td>");
writer.Write("Select Status");
writer.Write("</Td>");
writer.Write("<Td>");
ddlStatus.RenderControl(writer);
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td>");
writer.Write("Select Priority");
writer.Write("</Td>");
writer.Write("<Td>");
ddlPriority.RenderControl(writer);
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td>");
writer.Write("Select Start Date");
writer.Write("</Td>");
writer.Write("<Td>");
dtStartDate.RenderControl(writer);
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td>");
writer.Write("Select Due Date");
writer.Write("</Td>");
writer.Write("<Td>");
dtDueDate.RenderControl(writer);
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td Colspan='2' Align='Center'>");
btnCreateTask.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
{
txtTaskName = new TextBox();
txtTaskName.ID = "txtTaskName";
this.Controls.Add(txtTaskName);
peAssignedTo = new PeopleEditor();
peAssignedTo.ID = "peAssignedTo";
peAssignedTo.MultiSelect = false;
this.Controls.Add(peAssignedTo);
ddlStatus = new DropDownList();
ddlStatus.ID = "ddlStatus";
SPWeb currentWeb = SPControl.GetContextWeb(Context);
SPQuery sQuery = new SPQuery();
sQuery.Query = "<OrderBy><FieldRef Name='ID'
Ascending='True' /></OrderBy>";
SPList lstStatus = currentWeb.Lists["Status"];
SPListItemCollection myColl_Status =
lstStatus.GetItems(sQuery);
if (myColl_Status.Count > 0)
{
ddlStatus.DataValueField = "ID";
ddlStatus.DataTextField = "Title";
ddlStatus.DataSource = myColl_Status.GetDataTable();
ddlStatus.DataBind();
}
ddlStatus.Items.Insert(0, new ListItem("Select Status"));
this.Controls.Add(ddlStatus);
ddlPriority = new DropDownList();
ddlPriority.ID = "ddlPriority";
SPList lstPriority = currentWeb.Lists["Priority"];
SPListItemCollection myColl_Priority =
lstPriority.GetItems(sQuery);
if (myColl_Priority.Count > 0)
{
ddlPriority.DataValueField = "ID";
ddlPriority.DataTextField = "Title";
ddlPriority.DataSource = myColl_Priority.GetDataTable();
ddlPriority.DataBind();
}
ddlPriority.Items.Insert(0, new ListItem("Select
Priority"));
this.Controls.Add(ddlPriority);
dtStartDate = new DateTimeControl();
dtStartDate.ID = "dtStartDate";
this.Controls.Add(dtStartDate);
dtDueDate = new DateTimeControl();
dtDueDate.ID = "dtDueDate";
this.Controls.Add(dtDueDate);
btnCreateTask = new Button();
btnCreateTask.ID = "btnCreateTask";
btnCreateTask.Text = "Create Task";
btnCreateTask.Click += new
EventHandler(btnCreateTask_Click);
this.Controls.Add(btnCreateTask);
}
catch (Exception ex)
{
strError += ex.ToString();
}
}
void btnCreateTask_Click(object sender, EventArgs e)
{
try
{
SPWeb curentWeb = SPControl.GetContextWeb(Context);
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(curentWeb.Url))
{
using (SPWeb objWeb = site.OpenWeb())
{
objWeb.AllowUnsafeUpdates = true;
SPList lstTasks = curentWeb.Lists["Sample
Tasks"];
SPListItemCollection myColl = lstTasks.Items;
SPListItem item = myColl.Add();
item["Title"] = txtTaskName.Text;
item["AssignedTo"] =
objWeb.AllUsers[peAssignedTo.Accounts[0].ToString()];
if (ddlStatus.SelectedIndex != 0)
{
item["Status"] =
ddlStatus.SelectedValue.ToString()+";#"+ddlStatus.SelectedItem.Text;
}
if (ddlPriority.SelectedIndex !=0)
{
item["Priority"] =
ddlPriority.SelectedValue.ToString() + ";#"+ddlPriority.SelectedItem.Text;
}
item["StartDate"] =
Convert.ToDateTime(dtStartDate.SelectedDate.ToShortDateString());
item["DueDate"] =
Convert.ToDateTime(dtDueDate.SelectedDate.ToShortDateString());
item.Update();
strError += "Task Successfully Created...!";
Page.Response.Redirect(curentWeb.Url
+"/Lists/Sample Tasks/AllItems.aspx");
}
}
});
}
catch (Exception ex)
{
strError += ex.ToString();
}
}
}
}
No comments:
Post a Comment