Friday, 12 June 2009

CreatePage.aspx

I had a require the other day from a user, they wanted to remove certain page layouts from the CreatePage depending on a certain user.

We found that this couldn't be achieved with user permissions so instead I copied the CreatePage.aspx, inherited the class, and manually filtered the avaiable page layout.

If logic behind it goes, if the user belongs to a certain group then hide certain layouts from them.

The usergroup and layouts to hide come from the Web.config.

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;

namespace Leelodharry.Sharepoint
{
public class CreateIntranetPage : Microsoft.SharePoint.Publishing.Internal.CodeBehind.CreatePagePage
{

protected override void OnLoad(EventArgs e)
{
Microsoft.SharePoint.Publishing.Internal.WebControls.DropDownListWithDetails dd = this.pageTemplatePicker;


base.OnLoad(e);

if (!IsPostBack)
{
Microsoft.SharePoint.Publishing.Internal.WebControls.DropDownListWithDetails.ItemInfo[] ItemInfoArray = dd.DataList;

string id = dd.DropDownListId;
DropDownList drop = (DropDownList)dd.Controls[0].Controls[1];


System.Collections.IDictionary stsh = (System.Collections.IDictionary)System.Configuration.ConfigurationSettings.GetConfig("PageLayoutRestriction");
string FindGroup = (string)stsh["UserGroup"];


SPUser user = SPContext.Current.Web.CurrentUser;

bool IsAMember = UserIsMemberOf(user, FindGroup);

if (!IsAMember)
{
string LayoutAttributeValue = (string)stsh["CanSeeLayouts"];
string[] RestrictedLayouts = LayoutAttributeValue.Split(',');

for (int idx = 0; idx < RestrictedLayouts.Length; idx++)
{
string RemoveLayout = RestrictedLayouts[idx].Trim();

FindAndRemove(drop, RemoveLayout);
}

}



}

}


///
/// Checks to see if a user is part of a group
///

///
///
///
private bool UserIsMemberOf(SPUser user, string FindGroupName)
{
FindGroupName = FindGroupName.ToUpper();
int idx = 0;
bool Found = false;

while (!Found && idx < user.Groups.Count)
{
SPGroup group = user.Groups[idx];
if (group.Name.ToUpper() == FindGroupName)
{
Found = true;
}

idx++;
}

return Found;
}


///
/// Find and remove a string in a dropdown.
///

///
///
private void FindAndRemove(DropDownList dropdown, string FindText)
{
FindText = FindText.ToUpper();
bool Found = false;
int idx = 0;
while (!Found && idx < dropdown.Items.Count)
{
ListItem Item = dropdown.Items[idx];

string ItemText = Item.Text.ToUpper();

if (ItemText.Contains(FindText))
{
dropdown.Items.Remove(Item);
Found = true;
}

idx++;
}
}


}
}

0 comments:

Post a Comment