Hi,
Last week I got a new task i.e. in the web part it should display all the sub sites under the site collection.
If user has an access to the sub site, then it should display only Site Name, description and
If user hasn't got access to the site, then it should display only Site Name, description along with ‘Request To Join’ as below
If user clicks ‘Request To Join’ button, email has to be triggered to User group ‘Key Contacts’ members and maiBody,subject should be fetched from ‘EmailConfigurations’ list.
In that list, there will be column name as ‘Community Site Request To Join’
Mail Body, Mail Subject details should be fetched from the row as below
Below is the code that has been written to achieve this functionality
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls. WebParts;
using System.Data;
using System.Globalization;
using Microsoft.SharePoint;
using Microsoft.SharePoint. Administration;
using Microsoft.SharePoint. Utilities;
using System.Diagnostics;
using System.Text;
using System.Collections;
using System.Configuration;
using System.Data;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Collections. Specialized;
namespace DataListSample.VisualWebPart1
{
public partial class VisualWebPart1UserControl : UserControl
{
#region Variables
PagedDataSource PageDataSource= new PagedDataSource();
private DataTable dTableAllSites;
private DataTable dTableMatchingSites;
#endregion
#region Private Properties
private int CurrentPage
{
get
{
object objPage = ViewState["_CurrentPage"];
int _CurrentPage = 0;
if (objPage == null)
{
_CurrentPage = 0;
}
else
{
_CurrentPage = (int)objPage;
}
return _CurrentPage;
}
set { ViewState["_CurrentPage"] = value; }
}
private int firstIndex
{
get
{
int _FirstIndex = 0;
if (ViewState["_FirstIndex"] == null)
{
_FirstIndex = 0;
}
else
{
_FirstIndex = Convert.ToInt32(ViewState["_ FirstIndex"]);
}
return _FirstIndex;
}
set { ViewState["_FirstIndex"] = value; }
}
private int lastIndex
{
get
{
int _LastIndex = 0;
if (ViewState["_LastIndex"] == null)
{
_LastIndex = 0;
}
else
{
_LastIndex = Convert.ToInt32(ViewState["_ LastIndex"]);
}
return _LastIndex;
}
set { ViewState["_LastIndex"] = value; }
}
#region Private Methods
/// <summary>
/// Build DataTable to bind Main Items List
/// </summary>
/// <returns>DataTable</returns>
private DataTable GetTable()
{
dTableAllSites = new DataTable();
dTableAllSites.Locale = CultureInfo.InvariantCulture;
dTableAllSites.Columns.Add(" SiteName", typeof(string));
dTableAllSites.Columns.Add(" Description", typeof(string));
dTableAllSites.Columns.Add(" Url", typeof(string));
dTableAllSites.Columns.Add(" RequestAccess", typeof(bool));
return dTableAllSites;
}
private DataTable GetTableForSearch()
{
dTableMatchingSites = new DataTable();
dTableMatchingSites.Locale = CultureInfo.InvariantCulture;
dTableMatchingSites.Columns. Add("SiteName", typeof(string));
dTableMatchingSites.Columns. Add("Description", typeof(string));
dTableMatchingSites.Columns. Add("Url", typeof(string));
dTableMatchingSites.Columns. Add("RequestAccess", typeof(bool));
return dTableMatchingSites;
}
#endregion
/// <summary>
/// Binding Main Items List
/// </summary>
private void BindItemsList()
{
dTableAllSites = GetSites();
dListItems.DataSource = dTableAllSites;
dListItems.DataBind();
PageDataSource.DataSource = dTableAllSites.DefaultView;
PageDataSource.AllowPaging = true;
PageDataSource.PageSize = 10;
PageDataSource. CurrentPageIndex = CurrentPage;
ViewState["TotalPages"] = PageDataSource.PageCount;
this.lblPageInfo. Text = (CurrentPage + 1) + " | " + PageDataSource.PageCount;
this.lbtnPrevious.Enabled = !PageDataSource.IsFirstPage;
this.lbtnNext.Enabled = !PageDataSource.IsLastPage;
this.dListItems.DataSource = PageDataSource;
this.dListItems.DataBind();
this.doPaging();
}
/// <summary>
/// Binding Paging List
/// </summary>
private void doPaging()
{
DataTable dtPaging = new DataTable();
dtPaging.Columns.Add(" PageIndex");
dtPaging.Columns.Add(" PageText");
firstIndex = CurrentPage - 5;
if (CurrentPage > 5)
{
lastIndex = CurrentPage + 5;
}
else
{
lastIndex = 10;
}
if (lastIndex > Convert.ToInt32(ViewState[" TotalPages"]))
{
lastIndex = Convert.ToInt32(ViewState[" TotalPages"]);
firstIndex = lastIndex - 10;
}
if (firstIndex < 0)
{
firstIndex = 0;
}
for (int i = firstIndex; i < lastIndex; i++)
{
DataRow dr = dtPaging.NewRow();
dr[0] = i;
dr[1] = i + 1;
dtPaging.Rows.Add(dr);
}
this.dlPaging.DataSource = dtPaging;
this.dlPaging.DataBind();
}
private DataTable GetSites()
{
SPSecurity. RunWithElevatedPrivileges(dele gate()
{
using (SPSite site = new SPSite(SPContext.Current.Web. Url))
{
using (SPWeb oWebsite = site.OpenWeb())
{
dTableAllSites = GetTable();
foreach (SPWeb subSite in oWebsite.Webs)
{
DataRow dtRow = null;
dtRow = dTableAllSites.NewRow();
if (subSite.Title != null)
{
}
if (subSite.Description != null)
{
}
if (subSite.Url != null)
{
}
if (subSite. DoesUserHavePermissions(SPCont ext.Current.Web.CurrentUser. LoginName,SPBasePermissions.Open) == true)
{
}
else
{
}
dTableAllSites.Rows.Add(dtRow) ;
}
}
}
});
dTableAllSites.AcceptChanges() ;
return dTableAllSites;
}
private DataTable GetMatchingSites()
{
SPSecurity. RunWithElevatedPrivileges(dele gate()
{
using (SPSite site = new SPSite(SPContext.Current.Web. Url))
{
using (SPWeb oWebsite = site.OpenWeb())
{
dTableMatchingSites = GetTableForSearch();
foreach (SPWeb subSite in oWebsite.Webs)
{
DataRow dtRow = null;
if ((subSite.Title.ToLower()). Contains(txtSites.Text. ToLower()))
{
}
}
}
}
});
dTableMatchingSites. AcceptChanges();
return dTableMatchingSites;
}
#endregion
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindItemsList();
}
}
protected void ImgSearch_Click(object sender, ImageClickEventArgs e)
{
dTableMatchingSites = GetMatchingSites();
dListItems.DataSource = dTableMatchingSites;
dListItems.DataBind();
}
protected void lbtnNext_Click(object sender, EventArgs e)
{
CurrentPage += 1;
this.BindItemsList();
}
protected void lbtnPrevious_Click(object sender, EventArgs e)
{
CurrentPage -= 1;
this.BindItemsList();
}
protected void lbtnRequestToJoin_Click(object sender, EventArgs e)
{
using (SPSite site = new SPSite(SPContext.Current.Site. Url))
{
using (SPWeb web = site.OpenWeb())
{
SPGroup grp = web.SiteGroups["KeyContacts"];
{
foreach (SPUser user in grp.Users)
{
SPList objlist = web.Lists[" EmailConfigurations"];
foreach (SPListItem item in objlist.Items)
{
headers.Add("To", user.Email);
}
}
}
}
}
}
}
}
Testing
Coming to testing part,before testing below steps have been followed to view the result
1. User id needs to be added as site collection administrator
2. User id needs to be added in the ‘Owner’ group of the site collection
3. In all the subsites ‘Stop inheriting permissions’ have been removed all groups have been removed by ‘Remove permissions’
4. In some subsites User id has been added by providing ‘Full Control’
5. User id has been removed as administrator of the site collection and web part has been added to site collection page
No comments:
Post a Comment