Monday, March 8, 2010

DiscussionListSelectorWebPart

using System;using System.Collections;using System.Collections.Generic;using System.Security.Permissions;using System.Text;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using Microsoft.SharePoint;using Microsoft.SharePoint.WebControls;using System.Xml.Serialization;using System.Data;using System.Web.UI.HtmlControls;using System.Runtime.InteropServices;using DiscussionListSelector.WebPartCode;
namespace DiscussionListSelector{ [Guid("bbabc7fa-982c-4d13-81f8-2165eeacaa61")] public class DiscussionListSelector : Microsoft.SharePoint.WebPartPages.WebPart { private CustomValidator validator; protected LiteralControl openEditorPartControl; protected HtmlTable htmTbl; private Guid guidList; private string selectedWebUrl; private string ItmChkBox; SPGridView grdView; private ObjectDataSource gridDS; // public DataTable newtbl = new DataTable();
#region Properties
public SPWeb SelectedWeb { get { if (string.IsNullOrEmpty(SelectedWebUrl)) { return SPContext.Current.Web; } else { SPWeb currentWeb;
try { currentWeb = new SPSite(SelectedWebUrl).OpenWeb(); if (currentWeb.DoesUserHavePermissions(SPBasePermissions.Open)) { return currentWeb; } else { return null; } } catch (Exception) { return null; } } } }
private SPList SelectedCheckList { get { if (guidList != Guid.Empty) { if (SelectedWeb != null) { return this.SelectedWeb.Lists[this.guidList]; } else { return null; } } else { return null; } } }

[Personalizable(PersonalizationScope.Shared), WebBrowsable(false), WebDisplayName("Selected Web URL")] public string SelectedWebUrl { get { return this.selectedWebUrl; } set { this.selectedWebUrl = value; } }
[Personalizable(PersonalizationScope.Shared), WebBrowsable(false), WebDisplayName("Selected Itme Cheked Box value"), WebDescription("Selected Itme Cheked Box value")] public string SelectedItmChkBox { get { if (String.IsNullOrEmpty(ItmChkBox)) { return string.Empty; } else { return ItmChkBox; } } set { ItmChkBox = value; } } [Personalizable(PersonalizationScope.Shared), WebBrowsable(false), WebDisplayName("ListName"), WebDescription("ListName Property")] public string ListGuid { get { if (guidList == Guid.Empty) { return String.Empty; } else { return guidList.ToString(); } }
set { if (string.IsNullOrEmpty(value)) { guidList = Guid.Empty; } else { guidList = new Guid(value); } } }
#endregion
public override EditorPartCollection CreateEditorParts() { ArrayList editorArray = new ArrayList();
if (this.EffectiveStorage == Microsoft.SharePoint.WebPartPages.Storage.Shared) {
DiscussionListSelectorEditPart editorPart2 = new DiscussionListSelectorEditPart(); editorPart2.ID = this.ID + "_editorPart1"; editorArray.Add(editorPart2); }
EditorPartCollection editorParts = new EditorPartCollection(editorArray); return editorParts; }
public DiscussionListSelector() { this.ExportMode = WebPartExportMode.All; }
public override void RenderControl(System.Web.UI.HtmlTextWriter writer) {
grdView.DataBind(); EnsureChildControls(); try { base.Render(writer); } catch (Exception exception) { writer.WriteLine("The following error occurred in this Web Part: {0}.\nPlease contact your administrator for further assistance.", exception.Message); } }
protected override void CreateChildControls() { try {
SPSecurity.RunWithElevatedPrivileges(delegate() { validator = new CustomValidator(); validator.ID = "validator"; validator.IsValid = true; validator.ErrorMessage = String.Empty; validator.Display = ValidatorDisplay.Dynamic; Controls.Add(validator);
const string GRIDID = "grid"; const string DATASOURCEID = "gridDS";
gridDS = new ObjectDataSource(); gridDS.ID = DATASOURCEID; gridDS.SelectMethod = "SelectData"; gridDS.TypeName = this.GetType().AssemblyQualifiedName; gridDS.ObjectCreating += new ObjectDataSourceObjectEventHandler(gridDS_ObjectCreating); this.Controls.Add(gridDS);
grdView = new SPGridView(); grdView.ID = "MyGridView"; grdView.AutoGenerateColumns = false; grdView.EmptyDataText = "There are no Content Items";
BoundField col = new BoundField(); col.DataField = "RowID"; //"Discussion Icon"; col.HeaderText = "Discussion Icon"; col.HeaderStyle.CssClass = "ms-vh2"; col.HtmlEncode = false; grdView.Columns.Add(col); HyperLinkField hLinkCol = new HyperLinkField();
hLinkCol.DataTextField = "Name"; hLinkCol.HeaderText = "Name"; grdView.Columns.Add(hLinkCol);
BoundField colItem = new BoundField(); colItem.DataField = "Items"; colItem.HeaderText = "Items"; colItem.HeaderStyle.CssClass = "ms-vh2"; colItem.HtmlEncode = false; grdView.Columns.Add(colItem);
col = new BoundField(); col.DataField = "Last Modified"; col.SortExpression = "Last Modified"; col.HeaderText = "Last Modified"; grdView.Columns.Add(col);
grdView.AllowSorting = true; grdView.AllowFiltering = true; grdView.FilterDataFields = ",Name,"; //grdView.FilterDataFields = ",LoginName,Name,Group"; grdView.FilteredDataSourcePropertyName = "FilterExpression"; grdView.FilteredDataSourcePropertyFormat = "{1} LIKE '{0}'"; grdView.AllowPaging = false; grdView.AutoGenerateColumns = false; grdView.ID = GRIDID; grdView.DataSourceID = gridDS.ID;
// to persists filter Expression. when a sort action is performed
if (SelectedWeb == null) { validator.IsValid = false; return; } //SelectedCheckList if (SelectedWeb == null) { openEditorPartControl = new LiteralControl( string.Format( "Use the web part's Tool Pane to adjust its settings.", 1, 129, this.ID ) ); this.Controls.Add(openEditorPartControl); } else { // if (SelectedWebUrl != null) { SelectData();
} string url = String.Empty; grdView.RowDataBound += new GridViewRowEventHandler(grdView_RowDataBound);
System.Web.HttpRequest req = System.Web.HttpContext.Current.Request; if (req.Form["__CALLBACKID"] == null req.Form["__CALLBACKPARAM"] == null !req.Form["__CALLBACKID"].EndsWith("grid")) //GRIDID Group { if (ViewState["FilterExpression"] != null) gridDS.FilterExpression = (string)ViewState["FilterExpression"];
}

} }); Controls.Add(grdView); //grdView.DataBind(); base.CreateChildControls(); } catch (Exception exc) { validator.IsValid = false; ; validator.ErrorMessage = exc.Message; } }
//use a standard DataTable as the data source for our SPGridView //which we’ll bind to an ObjectDataSource(found in System.Web.UI.WebControls). public DataTable SelectData() {
DataTable newtbl = new DataTable(); SPSite MySite = SPContext.Current.Site; SPWeb CurrentSite = MySite.RootWeb; newtbl.Columns.Add("RowID", typeof(string)); newtbl.Columns.Add("Name", typeof(string)); newtbl.Columns.Add("Items", typeof(string)); newtbl.Columns.Add("Last Modified", typeof(string)); DataRow row; //SelectedItmChkBox
if (SelectedItmChkBox != "") //SelectedItmChkBox != null { string[] selectedDiscuLst = SelectedItmChkBox.Split(new char[] { ',' });//,new char()) int i; for (i = 0; i < selectedDiscuLst.Length; i++) { string sListName = selectedDiscuLst[i].Trim(); SPList forum = (SPList)CurrentSite.Lists[sListName]; int cin = forum.ItemCount; row = newtbl.Rows.Add(); row["RowID"] = forum.ID; // tbl.Rows[i]["Id"]; row["Name"] = forum.Title;// tbl.Rows[i]["Name"]; row["Items"] = cin; row["Last Modified"] = forum.LastItemModifiedDate; } } newtbl.AcceptChanges();
return newtbl; }
private void gridDS_ObjectCreating(object sender, ObjectDataSourceEventArgs e) { e.ObjectInstance = this; }
// protected void RadGridPubDocs_ItemDataBound(object sender, GridItemEventArgs e) //{ // try // { // if (e.Item.ItemType == GridItemType.Item e.Item.ItemType == GridItemType.AlternatingItem e.Item.ItemType == GridItemType.SelectedItem) // { // GridDataItem item = (GridDataItem)e.Item; // if (((DataRowView)item.DataItem).Row.Table.Columns.Contains("FILE_EXT")) // { // DataRowView dr = (DataRowView)dataItem.DataItem; // Image img = (Image)dataItem["ImageColumnUniqueName"].Controls[0]; // // set the image url by concatenating the image path with the datarowview values // img.ImageUrl = "~/Images/" + dr["FILE_EXT"].ToString(); // } // .....
// { // try // { // if (e.Item.ItemType == GridItemType.Item e.Item.ItemType == GridItemType.AlternatingItem e.Item.ItemType == GridItemType.SelectedItem) // { // GridDataItem item = (GridDataItem)e.Item; // if (((DataRowView)item.DataItem).Row.Table.Columns.Contains("FILE_EXT")) // { // // set the image here // }

protected void grdView_ItemDataBound(object sender, GridItemEventArgs e) { if (e.Item.ItemType == GridItemType.Item) { GridDataItem item = (GridDataIte)e.Item;
if (((DataRowView)item.DataItem).Row.Table.Columns.Contains("FILE_EXT")) { DataRowView dr = (DataRowView)dataItem.DataItem; Image img = (Image)dataItem["Discussion Icon"].Controls[0]; // set the image url by concatenating the image path with the datarowview values img.ImageUrl = "~/Images/" + dr["FILE_EXT"].ToString(); } } }
protected void grdView_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { DataRowView d = (DataRowView)e.Row.DataItem; string surl = d["Name"].ToString();

//TableCellCollection myCells = e.Row.Cells; HyperLink planLink = new HyperLink(); planLink = (HyperLink)e.Row.Cells[0].Controls[0]; planLink.ImageUrl = "~/Images/" + "itdisc.gif";

HyperLink hc = new HyperLink(); hc = (HyperLink)e.Row.Cells[1].Controls[0]; SPSite spsiteurl = SPContext.Current.Site; // string surl = spsiteurl.Url + "/" + SelectedList.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url;// +"?ID=" + rowIdNo;//dtr["ID"];
hc.NavigateUrl = spsiteurl.Url + "/Lists/" + surl;
//Image img = e.Row.FindControl("RowID") as Image; //img.ImageUrl = "~/Images/" + "itdisc.gif";
}
}
}
}

==================DiscussionListSelectorEditPart.cs ============

using System;using System.Collections.Generic;using System.Security.Permissions;using System.Text;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using Microsoft.SharePoint;using Microsoft.SharePoint.WebControls;
namespace DiscussionListSelector.WebPartCode{ class DiscussionListSelectorEditPart:EditorPart { private CustomValidator validator; //private DropDownList ddlLists; private CheckBoxList chkLst; private Label lblLists, lblWebUrl; private Table tblMain; private Button btnOpenWeb; private TextBox txtWebUrl;
#region Properties // private SPList SelectedList { get { return this.SelectedWeb.Lists[this.SelectedListId]; } }
public SPWeb SelectedWeb { get { if (string.IsNullOrEmpty(txtWebUrl.Text)) { return SPContext.Current.Web; } else { SPWeb webNew; try { webNew = new SPSite(txtWebUrl.Text).OpenWeb();
if (webNew.DoesUserHavePermissions(SPBasePermissions.Open)) { txtWebUrl.Text = webNew.Url; return webNew; } else { validator.IsValid = false; return null; } } catch (Exception exception) { validator.IsValid = false; validator.ErrorMessage = exception.Message; return null; } } } }
private Guid SelectedCheckListId { get { Guid guid = Guid.Empty;
if (ViewState["SelectedCheckListId"] == null) { guid = Guid.Empty; } else { guid = new Guid((string)ViewState["SelectedCheckListId"]); }
return guid; } set { ViewState["SelectedCheckListId"] = value.ToString(); } }

//private Guid SelectedListId //{ // get // { // Guid guid = Guid.Empty;
// if (ViewState["SelectedListId"] == null) // { // guid = Guid.Empty; // } // else // { // guid = new Guid((string)ViewState["SelectedListId"]); // }
// return guid; // } // set { ViewState["SelectedListId"] = value.ToString(); } //} public string SelectedWebUrl { get { string webUrl = String.Empty;
if (ViewState["SelectedWebUrl"] == null) { webUrl = String.Empty; } else { webUrl = (string)ViewState["SelectedWebUrl"]; }
return webUrl; } set { ViewState["SelectedWebUrl"] = value.ToString(); } }
protected string SelectedItmChkBox { get { string ItmChkBox = String.Empty;
if (ViewState["SelectedItmChkBox"] == null) { ItmChkBox = String.Empty; } else { ItmChkBox = (string)ViewState["SelectedItmChkBox"]; }
return ItmChkBox; } set { ViewState["SelectedItmChkBox"] = value.ToString(); } }
#endregion
public override void SyncChanges() { DiscussionListSelector parentWebPart = (DiscussionListSelector)this.WebPartToEdit;
if (!string.IsNullOrEmpty(parentWebPart.SelectedWebUrl)) { SelectedWebUrl = parentWebPart.SelectedWebUrl; }
if (parentWebPart.ListGuid != String.Empty) { //SelectedListId = new Guid(parentWebPart.ListGuid); } if (parentWebPart.SelectedItmChkBox != string.Empty parentWebPart.SelectedItmChkBox != "") { SelectedItmChkBox = parentWebPart.SelectedItmChkBox; string[] selectedDiscuLst = SelectedItmChkBox.Split(new char[] { ',' });//,new char()) int i; for (i = 0; i < selectedDiscuLst.Length; i++) //foreach (string sDissLst in selectedDiscuLst) { if (chkLst != null) { for (int j = 0; j < chkLst.Items.Count; j++) { if (chkLst.Items[j].Text == selectedDiscuLst[i]) { chkLst.Items[j].Selected = true; } } }
} }
}
public override bool ApplyChanges() {
DiscussionListSelector parentWebPart = (DiscussionListSelector)this.WebPartToEdit;
// Send the custom text to the Web Part. //if (ddlLists.SelectedValue != String.Empty) //{ // parentWebPart.ListGuid = ddlLists.SelectedValue; //} //else //{ // parentWebPart.ListGuid = String.Empty; //} parentWebPart.SelectedWebUrl = txtWebUrl.Text; SelectedItmChkBox = string.Empty; //CheckBoxList chkBoxList = sender as CheckBoxList; foreach (ListItem item in chkLst.Items) if (item.Selected) SelectedItmChkBox += "," + item;
if (SelectedItmChkBox.Length == 1 SelectedItmChkBox == "") SelectedItmChkBox = string.Empty; else { SelectedItmChkBox = SelectedItmChkBox.Remove(0, 1); } // ViewState["SelectedItmChkBox"] = SelectedItmChkBox;
parentWebPart.SelectedItmChkBox = SelectedItmChkBox; return true; }
protected override void CreateChildControls() { //CustomListViewWebPart parentWebPart = (CustomListViewWebPart)this.WebPartToEdit; validator = new CustomValidator(); validator.ID = "customValidator"; validator.IsValid = true; validator.ErrorMessage = String.Empty; validator.Display = ValidatorDisplay.Dynamic; chkLst = new CheckBoxList(); chkLst.ID = "chkLists"; chkLst.Visible = true; chkLst.AutoPostBack = true; chkLst.SelectedIndexChanged += new EventHandler(chkLst_SelectedIndexChanged);
// ddlLists = new DropDownList(); // ddlLists.ID = "ddlLists"; // ddlLists.Visible = true; // ddlLists.Enabled = true; //// ddlLists.AutoPostBack = true; // //ddlLists.SelectedIndexChanged += new EventHandler(OnListsChange);
// //ddlLists.SelectedItem = ListSelectionMode.Multiple;
lblLists = new Label(); lblLists.ID = "lblLists"; lblLists.Visible = true; lblLists.Text = "Lists:";
lblWebUrl = new Label(); lblWebUrl.Text = "Site URL:"; lblWebUrl.ID = "lblWebURL";
txtWebUrl = new TextBox(); txtWebUrl.ID = "txtWebUurl"; txtWebUrl.CssClass = "ms-input"; txtWebUrl.Width = Unit.Pixel(180); txtWebUrl.Text = SelectedWebUrl;
btnOpenWeb = new Button(); btnOpenWeb.Text = "Open"; btnOpenWeb.ID = "btnOpenWeb"; btnOpenWeb.Click += new EventHandler(btnOpenWeb_Click);
TableRow trTop = new TableRow(); TableCell tcTop = new TableCell(); tcTop.ColumnSpan = 2; tcTop.Controls.Add(validator); trTop.Cells.Add(tcTop);
TableRow trSelectWeb = new TableRow(); TableCell tcSelectWebLabel = new TableCell(); tcSelectWebLabel.Controls.Add(lblWebUrl); tcSelectWebLabel.Wrap = false; trSelectWeb.Cells.Add(tcSelectWebLabel); TableCell tcSelectedWebControl = new TableCell(); tcSelectedWebControl.Controls.Add(txtWebUrl); tcSelectedWebControl.Controls.Add(new LiteralControl(" ")); tcSelectedWebControl.Controls.Add(btnOpenWeb); //tcSelectedWebControl.Controls.Add(txtMaxLmt);
trSelectWeb.Cells.Add(tcSelectedWebControl);
TableRow trLists = new TableRow(); TableCell tcListsLabel = new TableCell(); tcListsLabel.Controls.Add(lblLists); tcListsLabel.Wrap = false; trLists.Cells.Add(tcListsLabel); TableCell tcListsControl = new TableCell(); //tcListsControl.Controls.Add(ddlLists); //trLists.Cells.Add(tcListsControl);
TableRow trCheckLists = new TableRow(); TableCell tccheckListsLabel = new TableCell(); tccheckListsLabel.Controls.Add(lblLists); tccheckListsLabel.Wrap = false; trCheckLists.Cells.Add(tccheckListsLabel);
TableCell tcCheckListsControl = new TableCell(); tcCheckListsControl.Controls.Add(chkLst); trCheckLists.Cells.Add(tcCheckListsControl);

tblMain = new Table(); tblMain.Rows.Add(trSelectWeb); tblMain.Rows.Add(trTop); // tblMain.Rows.Add(trLists); tblMain.Rows.Add(trCheckLists); // tblMain.Rows.Add(trMaxMoreLimitControl); //tblMain.Rows.Add(trFilterValue); try { Controls.Add(tblMain); PopulateCheckBox(); base.CreateChildControls(); } catch (Exception exc) { validator.IsValid = false; ; validator.ErrorMessage = exc.Message; } }
private void btnOpenWeb_Click(object sender, EventArgs e) { SelectedWebUrl = txtWebUrl.Text; PopulateCheckBox(); }
private void PopulateCheckBox() { if( chkLst != null ) chkLst.Items.Clear(); ListItem newItem; if (SelectedWeb == null) { chkLst.Enabled = false; return; } else { chkLst.Enabled = true; }
if (SelectedWebUrl != "") // txtWebUrl.Text { foreach (SPList item in this.SelectedWeb.Lists) { if (item.BaseTemplate == SPListTemplateType.DiscussionBoard) { if (item.Hidden == false) { newItem = new ListItem(); newItem.Text = item.Title; newItem.Value = item.ID.ToString(); //// if (this.SelectedListId == item.ID) // { // newItem.Selected = true; // }
//ddlLists.Items.Add(newItem); chkLst.Items.Add(newItem); } } }
//if (ddlLists.SelectedItem == null) if (chkLst == null) { //newItem = new ListItem(); //newItem.Text = String.Empty; //newItem.Value = String.Empty; //newItem.Selected = true; ////ddlLists.Items.Add(newItem); //chkLst.Items.Add(newItem); } else { //OnListsChange(ddlLists, new EventArgs()); chkLst_SelectedIndexChanged(chkLst, new EventArgs());
} } }
#region event handlers
private void chkLst_SelectedIndexChanged(object sender, EventArgs e) { //SelectedCheckListId = new Guid(chkLst.SelectedValue); // SPList currentList = SelectedList;
SelectedItmChkBox = string.Empty; CheckBoxList chkBoxList = sender as CheckBoxList; foreach (ListItem item in chkBoxList.Items) if (item.Selected ) SelectedItmChkBox += "," + item; if (SelectedItmChkBox.Length == 1 SelectedItmChkBox=="") SelectedItmChkBox = string.Empty; else { SelectedItmChkBox = SelectedItmChkBox.Remove(0, 1); } ViewState["SelectedItmChkBox"] = SelectedItmChkBox; } #endregion
protected override void RenderContents(System.Web.UI.HtmlTextWriter writer) {
//lblWebUrl.RenderControl(writer); //txtWebUrl.RenderControl(writer); //btnOpenWeb.RenderControl(writer); //tblMain.RenderControl(writer); //writer.Write("

"); //chkLst.RenderControl(writer); //writer.Write("
"); base.RenderContents(writer);
}
}}

No comments:

Post a Comment