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.Linq;
[assembly:AllowPartiallyTrustedCallers]
namespace getSubWebs
{
[Guid("f44bd7b7-14a3-4734-8716-7bb9dd724350")]
public class WebPart1 : System.Web.UI.WebControls.WebParts.WebPart
{
public WebPart1()
{
}
string strError = string.Empty;
DropDownList ddlSubWebs;
SPGridView sgvSubWebsData;
protected override void Render(HtmlTextWriter writer)
{
writer.Write(strError);
try
{
ddlSubWebs.RenderControl(writer);
sgvSubWebsData.RenderControl(writer);
}
catch (Exception ex)
{
strError += ex.ToString();
}
}
protected override void CreateChildControls()
{
try
{
ddlSubWebs = new DropDownList();
ddlSubWebs.AutoPostBack = true;
ddlSubWebs.SelectedIndexChanged += new
EventHandler(ddlSubWebs_SelectedIndexChanged);
this.Controls.Add(ddlSubWebs);
sgvSubWebsData = new SPGridView();
sgvSubWebsData.AutoGenerateColumns = false;
this.Controls.Add(sgvSubWebsData);
BoundField bTitle = new BoundField();
bTitle.HeaderText = "Title";
bTitle.DataField = "Title";
sgvSubWebsData.Columns.Add(bTitle);
BoundField bSubWebTitle = new BoundField();
bSubWebTitle.HeaderText = "SubWeb Name";
bSubWebTitle.DataField = "WebName";
sgvSubWebsData.Columns.Add(bSubWebTitle);
string Title = string.Empty;
string WebName = string.Empty;
string webUrl=string.Empty;
SPWeb currentWeb = SPContext.Current.Web;
var SubWebQuery=from SPWeb objWeb in
currentWeb.GetSubwebsForCurrentUser()
select new
{
WebName=objWeb.Title,
webUrl=objWeb.Url
};
if (SubWebQuery.Count() > 0)
{
ddlSubWebs.DataValueField = "webUrl";
ddlSubWebs.DataTextField = "WebName";
ddlSubWebs.DataSource = SubWebQuery;
ddlSubWebs.DataBind();
}
}
catch (Exception ex)
{
strError += ex.ToString();
}
}
void ddlSubWebs_SelectedIndexChanged(object sender, EventArgs
e)
{
try
{
string url = ddlSubWebs.SelectedValue;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite site = new SPSite(url);
SPWeb web = site.OpenWeb();
web.AllowUnsafeUpdates = true;
SPListItemCollection myColl =
web.Lists["Login"].Items;
var query = from SPListItem item in myColl
select new
{
Title = item["Title"].ToString(),
WebName = web.Title
};
sgvSubWebsData.DataSource = query;
sgvSubWebsData.DataBind();
});
}
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.Linq;
[assembly:AllowPartiallyTrustedCallers]
namespace getSubWebs
{
[Guid("f44bd7b7-14a3-4734-8716-7bb9dd724350")]
public class WebPart1 : System.Web.UI.WebControls.WebParts.WebPart
{
public WebPart1()
{
}
string strError = string.Empty;
DropDownList ddlSubWebs;
SPGridView sgvSubWebsData;
protected override void Render(HtmlTextWriter writer)
{
writer.Write(strError);
try
{
ddlSubWebs.RenderControl(writer);
sgvSubWebsData.RenderControl(writer);
}
catch (Exception ex)
{
strError += ex.ToString();
}
}
protected override void CreateChildControls()
{
try
{
ddlSubWebs = new DropDownList();
ddlSubWebs.AutoPostBack = true;
ddlSubWebs.SelectedIndexChanged += new
EventHandler(ddlSubWebs_SelectedIndexChanged);
this.Controls.Add(ddlSubWebs);
sgvSubWebsData = new SPGridView();
sgvSubWebsData.AutoGenerateColumns = false;
this.Controls.Add(sgvSubWebsData);
BoundField bTitle = new BoundField();
bTitle.HeaderText = "Title";
bTitle.DataField = "Title";
sgvSubWebsData.Columns.Add(bTitle);
BoundField bSubWebTitle = new BoundField();
bSubWebTitle.HeaderText = "SubWeb Name";
bSubWebTitle.DataField = "WebName";
sgvSubWebsData.Columns.Add(bSubWebTitle);
string Title = string.Empty;
string WebName = string.Empty;
string webUrl=string.Empty;
SPWeb currentWeb = SPContext.Current.Web;
var SubWebQuery=from SPWeb objWeb in
currentWeb.GetSubwebsForCurrentUser()
select new
{
WebName=objWeb.Title,
webUrl=objWeb.Url
};
if (SubWebQuery.Count() > 0)
{
ddlSubWebs.DataValueField = "webUrl";
ddlSubWebs.DataTextField = "WebName";
ddlSubWebs.DataSource = SubWebQuery;
ddlSubWebs.DataBind();
}
}
catch (Exception ex)
{
strError += ex.ToString();
}
}
void ddlSubWebs_SelectedIndexChanged(object sender, EventArgs
e)
{
try
{
string url = ddlSubWebs.SelectedValue;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite site = new SPSite(url);
SPWeb web = site.OpenWeb();
web.AllowUnsafeUpdates = true;
SPListItemCollection myColl =
web.Lists["Login"].Items;
var query = from SPListItem item in myColl
select new
{
Title = item["Title"].ToString(),
WebName = web.Title
};
sgvSubWebsData.DataSource = query;
sgvSubWebsData.DataBind();
});
}
catch (Exception ex)
{
strError += ex.ToString();
}
}
}
Hey Dude dont post lsrge chunks of code. Place short snippets so that the specific code is found easily. otherwise the user will have to browse through the whole code to find what he is looking for
ReplyDelete