Tuesday, June 15, 2010

Matching the site urls with list item urls

Recently we had an requirement of webpart where it should fetch all the site under that Web Application and check the urls from the list where some urls are already saved.After checking it should display URL's that are available in the list,List of all the sites under the current Web Application,sites that are not available in the list,list URL's are not available in the current Web Application and soon as user clicks the Delete button it should delete the urls in the list which are not alive

using System;

using System.Linq;

using System.Text;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

using System.Collections;

using System.Collections.Generic;

using System.Collections.Specialized;

using System.IO;

using Microsoft.SharePoint;

using Microsoft.SharePoint.Administration;

using Microsoft.SharePoint.WebPartPages;

using System.Web.UI.WebControls.WebParts;

namespace RequestSiteDocument

{

public class RequestSiteDocument : System.Web.UI.WebControls.WebParts.WebPart

{

Button btnCreate;

Button btnDelete;

Label lblException;

private bool customMessage = true;


SPWeb oweb = SPContext.Current.Web;

SPSite objsite = SPContext.Current.Site;

SPList objlist = null;



ArrayList arrListofWebHierarchy = new ArrayList();

ArrayList arrListURLInfo = new ArrayList();

ArrayList arrListofsitenames = new ArrayList();

ArrayList arrListURLInfoinDeletebutton = new ArrayList();

ArrayList arrListListofWebHierarchyinDeletebutton = new ArrayList();

ArrayList arrListfordeletelist = new ArrayList();

ArrayList arrListofsitenamesinDeletebutton = new ArrayList();


protected override void CreateChildControls()

{

base.CreateChildControls();



if (DisplayButton)

{

btnCreate = new Button();

btnCreate.ID = "btnCreate";

btnCreate.Text = "Create";

btnCreate.Click += new EventHandler(btnCreate_Click);

btnCreate.Visible = true;

this.Controls.Add(btnCreate);

btnDelete = new Button();

btnDelete.ID = "btnDelete";

btnDelete.Text = "Delete";

btnDelete.Click += new EventHandler(btnDelete_Click);

btnDelete.Visible = true;

this.Controls.Add(btnDelete);

}

lblException = new Label();

lblException.ID = "";

lblException.Visible = true;

this.Controls.Add(lblException);

}

void btnCreate_Click(object sender, EventArgs e)

{

throw new NotImplementedException();

}



[Personalizable(PersonalizationScope.Shared),

WebBrowsable(true),

WebDisplayName("Display button"),

WebDescription("Display button"),

SPWebCategoryName("Display button")]

public bool DisplayButton

{

get { return customMessage; }

set { customMessage = value; }

}



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

{

base.Render(writer);

//SPSite objsite = SPContext.Current.Site;


SPWebCollection objspw = objsite.AllWebs;

try

{

objlist = oweb.Lists["RequestASiteList"];

if (objlist != null)

{

writer.Write("");

writer.Write("");

writer.Write("");

writer.Write("");

foreach (SPListItem itmlist in objlist.Items)

{

writer.Write("");

writer.Write("");

writer.Write("");

arrListURLInfo.Add(strurl);

writer.Write("");

}

writer.Write("
URL's that are available in the list
Site TitleSite URL
");

string strsitetitle = (itmlist["Title"].ToString().ToLower());

writer.Write(strsitetitle);

writer.Write("
");

string strurl = (itmlist["url"].ToString()).ToLower();

writer.Write(strurl);

writer.Write("
");

}

writer.Write("");

writer.Write("");

writer.Write("");

writer.Write("");

//fetching all the site names and site urls

foreach (SPWeb objweb in objspw)

{

writer.Write("");

writer.Write("");

writer.Write("");

writer.Write("");

//adding all site url's to the array list called arrListofWebHierarchy

arrListofWebHierarchy.Add(objwebfullurl);

arrListofsitenames.Add(objweb.Title);

}

writer.Write("
List of all the sites under the current Web Application
Site NameSite URL
");

writer.Write(objweb.Title);

writer.Write("
");

writer.Write(objweb.Url);

string objwebfullurl = (objweb.Url).ToLower() + "/default.aspx";

writer.Write("
");

writer.Write("");

writer.Write("Below sites are not available in the list");

int sitefoundflag = 0;

//Compare arrListofWebHierarchy with arrListURLInfo

for (int i = 0; i < arrListofWebHierarchy.Count; i++) { for (int j = 0; j < arrListURLInfo.Count; j++) { if (arrListofWebHierarchy[i].ToString().CompareTo(arrListURLInfo[j].ToString().Trim()) == 0) { sitefoundflag = sitefoundflag + 1; } } if (sitefoundflag <= 0) { writer.Write("
");

writer.Write("Site Name:" + arrListofsitenames[i]);

writer.Write("");

writer.Write("URL:" + arrListofWebHierarchy[i]);

writer.Write("");

}

else

{

sitefoundflag = 0;

}

}

writer.Write("");

writer.Write("Below list URL's are not available in the current Web Application");

int listflag = 0;

//Compare arrListURLInfo with arrListofWebHierarchy

for (int i = 0; i < arrListURLInfo.Count; i++) { for (int j = 0; j < arrListofWebHierarchy.Count; j++) { if (arrListURLInfo[i].ToString().CompareTo(arrListofWebHierarchy[j].ToString().Trim()) == 0) { listflag = listflag + 1; } } if (listflag <= 0) { writer.Write("
");

writer.Write(arrListURLInfo[i]);

arrListfordeletelist.Add(arrListURLInfo[i]);

writer.Write("");

}

else

{

listflag = 0;

}

}

}

catch (Exception ex)

{

lblException = new Label();

lblException.Text = ex.ToString();

}

}


void btnDelete_Click(object sender, EventArgs e)

{

try

{

SPWebCollection objspw = objsite.AllWebs;

//get the url's that are available in the list field called url

objlist = oweb.Lists["RequestASiteList"];

if (objlist != null)

{

foreach (SPListItem itmlist in objlist.Items)

{

string strurl = (itmlist["url"].ToString()).ToLower();

arrListURLInfoinDeletebutton.Add(strurl);

}

}

//fetching all the site names and site urls

foreach (SPWeb objweb in objspw)

{

string objwebfullurl = (objweb.Url).ToLower() + "/default.aspx";

//adding all site url's to the array list called arrListListofWebHierarchyinDeletebutton

arrListListofWebHierarchyinDeletebutton.Add(objwebfullurl);

arrListofsitenamesinDeletebutton.Add(objweb.Title);

}

int listflag = 0;

//Compare arrListURLInfoinDeletebutton with arrListListofWebHierarchyinDeletebutton

for (int i = 0; i < arrListURLInfoinDeletebutton.Count; i++)

{

for (int j = 0; j < arrListListofWebHierarchyinDeletebutton.Count; j++)

{

if (arrListURLInfoinDeletebutton[i].ToString().CompareTo(arrListListofWebHierarchyinDeletebutton[j].ToString().Trim()) == 0)

{

listflag = listflag + 1;

}

}

if (listflag <= 0)

{

arrListfordeletelist.Add(arrListURLInfoinDeletebutton[i]);

}

else

{

listflag = 0;

}

}

SPList olist = oweb.Lists["RequestASiteList"];

SPListItemCollection itmcollection = olist.Items;

for (int i = 0; i < arrListfordeletelist.Count; i++)

{

for (int j = 0; j < itmcollection.Count; j++)

{

SPListItem objsplistitem = itmcollection[j];

if (arrListfordeletelist[i].ToString() == (objsplistitem["url"].ToString()).ToLower())

{

itmcollection.Delete(j);

}

}

}

}

catch (Exception ex)

{

lblException.Text = ex.ToString();

}


}

}

}

No comments:

Post a Comment