Friday, February 20, 2009

WebPart for adding Item To Bug List.

Хочу выложить код, в своё время с котором немного намучился из за каламбура со связанными списками.
Задача стояла такая: Есть список для логирования багов, самой форме слишком много полей которые не всегда есть время заполнять, а хочется в момент ввести данные и отправить их. Потому предстояло сделать вэб часть, в которую можно было бы ввести данные и добавить новый элемент.
Код:



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 Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace ELEKS.SharePoint.WebParts
{
[Guid("28ce955d-63e9-4681-b02c-d0583a25e0e3")]
public class QuickBugLog : System.Web.UI.WebControls.WebParts.WebPart
{
private Microsoft.SharePoint.SPWeb web = null;
private Microsoft.SharePoint.SPList bugsList = null;
private Microsoft.SharePoint.SPList projectsList = null;

protected TextBox txtBugTitle;
protected TextBox txtReproSteps;
protected DropDownList ddlProject;
protected Label lblValidationError;
protected Button btnBugLog;



public QuickBugLog()
{
}

protected override void CreateChildControls()
{
try
{

//initialize label
lblValidationError = new Label();
lblValidationError.CssClass = "ms-formvalidation";
Controls.Add(lblValidationError);

//initialize textboxes
txtBugTitle = new TextBox();
txtReproSteps = new TextBox();
txtReproSteps.TextMode = TextBoxMode.MultiLine;
txtReproSteps.Columns = 40;
txtReproSteps.Rows = 8;
Controls.Add(txtReproSteps);
Controls.Add(txtBugTitle);

//initialize button
btnBugLog = new Button();
btnBugLog.Text = "Log Bug";
btnBugLog.Click += new EventHandler(btnBugLog_Click);
Controls.Add(btnBugLog);

//initialize comboboxes
ddlProject = new DropDownList();
Controls.Add(ddlProject);

}
catch
{
this.lblValidationError.Text += "Error 1 : Can't Initialize controls.";
}



try
{
//get the web
this.web = Microsoft.SharePoint.SPContext.Current.Web;
web.AllowUnsafeUpdates = true;
//get the bugsheet list
this.bugsList = Microsoft.SharePoint.SPContext.Current.Web.Lists["Bugs"];
//get the projects list
this.projectsList = Microsoft.SharePoint.SPContext.Current.Web.GetList(web.ServerRelativeUrl + "/Lists/Projects/");

}
catch
{
this.lblValidationError.Text += "Error 0 : Can't connect to the curent context.";

}
try
{
//write CALM query for filtering data in list, to display active projects
string strCalmQuery =
"<Query>" +
"<OrderBy>" +
"<FieldRef Name='Title' />" +
"</OrderBy>" +
"<Where>" +
"<Geq>" +
"<FieldRef Name='EndDate' />" +
"<Value Type='DateTime'>" + System.DateTime.Today.ToString("yyyy/MM/dd HH:mm:ss") + "</Value>" +
"</Geq>" +
"</Where>" +
"</Query>";
SPQuery query = new SPQuery();
query.Query = strCalmQuery;
//Filter Project list Using CALM query
SPListItemCollection filteredItems = this.projectsList.GetItems(query);
if (filteredItems.Count != 0)
{
foreach (SPListItem item in filteredItems)
{
ListItem li = new ListItem(item.DisplayName.ToString());
ddlProject.Items.Add(li);
}
}
else
{
this.lblValidationError.Text = "Projects List is empty.";
}
}
catch
{
this.lblValidationError.Text += "Error 2 : Can't Filter Data With this CALM query.";
}


}
protected override void Render(System.Web.UI.HtmlTextWriter writer)

{


try
{
writer.Write("<table width=100%>");
writer.Write("<tr>");
writer.Write("<td colspan=2>");
lblValidationError.RenderControl(writer);
writer.Write("</td>");
writer.Write("</tr>");
writer.Write("<tr>");
writer.Write("<td>Bug Name:<span class=’ms-formvalidation’>*</span></td>");
writer.Write("<td>");
txtBugTitle.RenderControl(writer);
writer.Write("</td>");
writer.Write("</tr>");
writer.Write("<tr>");
writer.Write("<td>Project:<span class=’ms-formvalidation’>*</span></td>");
writer.Write("<td>");
ddlProject.RenderControl(writer);
writer.Write("</td>");
writer.Write("</tr>");
writer.Write("<tr>");
writer.Write("<td>Repro Steps: <span class=’ms-formvalidation’>*</span></td>");
writer.Write("<td>");
txtReproSteps.RenderControl(writer);
writer.Write("</td>");
writer.Write("</tr>");
writer.Write("<tr>");
writer.Write("<td></td>");
writer.Write("<td>");
writer.Write("</td>");
writer.Write("</tr>");
writer.Write("<tr>");
writer.Write("<td></td>");
writer.Write("<td>");
writer.Write("</td>");
writer.Write("</tr>");
writer.Write("<tr>");
writer.Write("<td>");
writer.Write("</td>");
writer.Write("<td>");
btnBugLog.RenderControl(writer);
writer.Write("</td>");
writer.Write("</tr>");
writer.Write("</table>");
}
catch
{
this.lblValidationError.Text += "Error 3 : Can't Render controls.";
}
}


public void btnBugLog_Click(object sender, EventArgs e)
{
try
{
string strCalmQuery_ProjectSelected =
"<Query>" +
"<Where>" +
"<Geq>" +
"<FieldRef Name='Title' />" +
"<Value Type='Text'>" + ddlProject.SelectedItem.Text.ToString() + "</Value>" +
"</Geq>" +
"</Where>" +
"</Query>";
SPQuery query_ProjectSelected = new SPQuery();
query_ProjectSelected.Query = strCalmQuery_ProjectSelected;
int ProjectSelected_id = 0;
//Filter Project list Using CALM query
SPListItemCollection filteredItems = this.projectsList.GetItems(query_ProjectSelected);
if (filteredItems.Count != 0)
{
ProjectSelected_id = filteredItems[0].ID;
}

if ((txtBugTitle.Text.ToString() != "") & (txtReproSteps.Text.ToString()!=""))
{

SPListItem item = this.bugsList.Items.Add();
item["Title"] = txtBugTitle.Text.ToString();
item["Repro Steps"] = txtReproSteps.Text.ToString();
item["Assigned To"] = "";
item["Resolved By"] = "";
item["Resolution"] = "";
item["Status"] = 2;
item["Priority"] = 2;
item["Fix By"] = "";
item["Project"] = new SPFieldLookupValue(ProjectSelected_id, ddlProject.SelectedItem.Text.ToString());
item["Category"] = 2;
item["Comments"] = "";
item["Created By"] = "";
item.Update();
this.lblValidationError.Text = "Bug: " + item.Title.ToString() + " loged at: " + System.DateTime.Today.ToString("yyyy/MM/dd HH:mm:ss");
txtBugTitle.Text = "";
txtReproSteps.Text = "";
}
else
{
this.lblValidationError.Text = "Fill in all fields to log a bug.";
}
}
catch
{
this.lblValidationError.Text += "Error 4 : Can't add Item to the bug list.";
}
}

}
}

1 comment:

  1. Well, Great! Interestingly written info. Turning attention on cheapest solutions in homeowners insurance, it is the right way to receive cheapest home owners insurance.

    ReplyDelete