onsdag 10. juni 2009

Programatically adding favorites in EPiServer

Sometimes if you are working with large EPiServer projects with a large number sites it can be abit overwhelming for the editor to have to manually browse through the entire structure of sites to find correct site.

Recently I was working on an EPiServer project with a total of 130 sites in the page tree structure. Each of these sites has its own designated editor with editing right just for that one site.

It became clear early that we needed a solution to help assist the editors find their site without wasting lots of time browsing the entire structure.

We solved this issue by creating a scheduled task in EPiServer that automatically would add favorites to editors based on their individual access rights.

While I was trying to find a way to do this the great oracle known google came up short. I figure maybe someone else might be interested in doing something similar so it might be a good idea post a blog about it.



Place these values under the configuration node in the web.config file. Change the ID out with your own.



<appSettings>
<add key="StartPagePageId" value="26" />>
</appSettings>



Here is the c# code you need:



[EPiServer.PlugIn.ScheduledPlugIn(
DisplayName = "Generate User Favorites",
Description = "A scheduled job to generate user favorites.",
SortIndex = 1)]
public class GenerateFavorites
{
public static string Execute()
{
if (ConfigurationManager.AppSettings["StartPagePageId"] != null)
{
///First we find collect the Id of the main root element containing all the site
///This is in our project collected from the web.config file */
PageReference pageReference =
new PageReference(int.Parse
(ConfigurationManager.AppSettings["StartPagePageId"]));

///Next we create a filter for search so that we will only retrieve the kind of
///pages we want, in our case the page type is called "Site"
PropertyCriteriaCollection propCritCol = new PropertyCriteriaCollection();
PropertyCriteria criteria = new PropertyCriteria
{
Type = PropertyDataType.String,
Name = "PageTypeName",
Value = "Site",
};
propCritCol.Add(criteria);

///Next we collect all the pages based on our previous code
PageDataCollection pageDataCollection =
DataFactory.Instance.FindPagesWithCriteria(pageReference, propCritCol);

///Next we iterate through each page in our collection and find all user
///that has explit rights to the page.
foreach (PageData page in pageDataCollection)
{
RawACE[] acls = page.ACL.ToRawACEArray();
foreach (RawACE acl in acls)
{
///Sets the entry type to user
if (acl.AutomaticEntryType == SecurityEntityType.User)
{
IPrincipal principal = GetPrincipalUser(acl.Name);
if (principal != null)
{
///Query the access rights of the user
if (page.ACL.QueryDistinctAccess(principal, AccessLevel.Publish))
{
EPiServerProfile profile =
EPiServerProfile.Get(principal.Identity.Name);

///If the user allready has favorites added we dont want
///to override those so we instead add them.
if (!string.IsNullOrEmpty(profile.CustomExplorerTreePanel))
{
if (!profile.CustomExplorerTreePanel.Contains
(page.PageLink.ID.ToString()))
profile.CustomExplorerTreePanel += ","
+ page.PageLink.ID;

profile.Save();
}
else
{
profile.CustomExplorerTreePanel =
page.PageLink.ID.ToString();
profile.Save();
}
}
}
}
}
}

return "MachineName:" + System.Environment.MachineName +
", Favorites updated succesfully";
}
return "MachineName:" + System.Environment.MachineName +
", Missing startPagePageId in AppSettings.";
}

public static IPrincipal GetPrincipalUser(string userName)
{
MembershipUser user = CurrentMembershipProvider.GetUser(userName, false);
if (user != null)
{
GenericIdentity identity = new GenericIdentity(user.UserName, user.ProviderName);
GenericPrincipal principal =
new GenericPrincipal(identity, Roles.GetRolesForUser(user.UserName));
return principal;
}
return null;
}
}