Parmi les trucs que je dois recoder à longueur de journée et dont je voudrais bien me passer : Faire le nettoyage (Supprimer) des éléments uploadés par une feature.
Pour rappel, si je souhaite uploader plusieurs Web Part dans SharePoint, je vais déclarer la feature suivante (d'exemple) :
Feature.xml : (NB : Les attributs ReceiverAssembly et ReceiverClass ne sont pas necessaire pour une feature de base)
<?xml version="1.0" encoding="utf-8" ?>
<Feature xmlns="http://schemas.microsoft.com/sharepoint/"
Id="C3C567EA-AC7B-402F-A328-F1AF9F9849F5"
Title="Hello World WP"
Description="Affiche une WebPart qui 'HelloWorld'"
Scope="Site"
Hidden="False"
Version="1.0.0.0"
ReceiverAssembly="HelloWorld, Version=1.0.0.0, Culture=neutral, PublicKeyToken=129475d183be15e5"
ReceiverClass="HelloWorld.FeatureReceiver">
<ElementManifests>
<ElementManifest Location="elements.xml" />
</ElementManifests>
</Feature>
Elements.xml :
<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Module Url="_catalogs/wp"
Path="WP"
RootWebOnly="TRUE">
<File Url="EasyWebPart1.webpart"
Type="GhostableInLibrary">
<Property Name="Group" Value="Hello World" />
<Property Name="Title" Value="HelloWorld 1" />
</File>
<File Url="EasyWebPart2.webpart"
Type="GhostableInLibrary">
<Property Name="Group" Value="Hello World" />
<Property Name="Title" Value="HelloWorld 2" />
</File>
</Module>
<Module Url="_catalogs/wp"
Path="WP"
RootWebOnly="TRUE">
<File Url="EasyWebPart3.webpart"
Type="GhostableInLibrary">
<Property Name="Group" Value="Hello World" />
<Property Name="Title" Value="HelloWorld 3" />
</File>
<File Url="EasyWebPart4.webpart"
Type="GhostableInLibrary">
<Property Name="Group" Value="Hello World" />
<Property Name="Title" Value="HelloWorld 4" />
</File>
</Module>
</Elements>
En pratique, je crée deux sections "modules" qui uploadent chacune 2 webpart dans le répertoire "_catalog/wp" de ma collection de site. Ce répertoire étant au final l'endroit où sont stockées les Web Part dans SharePoint.
Maintenant si j'active cette feature, je vais bien avoir mes 4 Web Part uploadées où il faut :
Mais si je désactive la feature, les fichiers sont toujours là...
En pratique ce n'est pas toujours une mauvaise chose mais dans certains cas (souvent), on peut vouloir nettoyer tout ça via un FeatureReceiver sur la désactivation de la feature. Et ca peut vite devenir rébarbatif quand on upload tout les 4 matins des Web Parts.
D'où la création d'une méthode d'extension pour me simplifier tout ça :)
En pratique, voilà comment je fais :
Dans mon Feature Receiver, je fais simplement appel à la méthode DeleteUploadedItems directement à partir du SPFeatureReceiverProperties qui m'est fourni par la méthode FeatureDeactivating.
class FeatureReceiver : SPFeatureReceiver
{
#region NotImplemented
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
//throw new NotImplementedException();
}
public override void FeatureInstalled(SPFeatureReceiverProperties properties)
{
//throw new NotImplementedException();
}
public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
{
//throw new NotImplementedException();
}
#endregion
/// <summary>
/// Occurs when a Feature is deactivated.
/// </summary>
/// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> object that represents the properties of the event.</param>
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
properties.DeleteUploadedFiles();
}
}
et voici le code de la méthode qui nous intéresse au final :
/// <summary>
/// Deletes the uploaded files. (Inspired from a Robert L. Bogue’s code | http://www.thorprojects.com/blog)
/// </summary>
/// <param name="properties">The properties.</param>
public static void DeleteUploadedFiles(this SPFeatureReceiverProperties properties)
{
SPWeb web = null;
switch (properties.Definition.Scope)
{
case SPFeatureScope.Site:
SPSite site = (SPSite)properties.Feature.Parent;
web = site.RootWeb;
break;
case SPFeatureScope.Web:
web = (SPWeb)properties.Feature.Parent;
break;
default:
return;
}
foreach (SPElementDefinition element in properties.Definition.GetElementDefinitions(CultureInfo.CurrentCulture))
{
if (element.ElementType == "Module")
{
string fileUrl = string.Empty;
XmlNode moduleNode = element.XmlDefinition;
if (moduleNode.Attributes["Url"] != null)
fileUrl = moduleNode.Attributes["Url"].Value + '/';
XmlNodeList fileNodes = moduleNode.SelectNodes("*[local-name()='File']");
if (fileNodes != null)
{
foreach (XmlNode fileNode in fileNodes)
{
string fullFileUrl = SPUrlUtility.CombineUrl(fileUrl, fileNode.Attributes["Url"].Value);
SPFile file = web.GetFile(fullFileUrl);
if (file != null && file.Exists)
file.Delete();
}
}
}
}
}
On récupère les SPElementDefinition, vérifie qu'il s'agit d'un type "Module", récupère le dossier où tout est uploadé puis le chemin complet pour accèder au fichier. Si le fichier existe, on le supprime.
PS : Ce bout de code est librement inspiré d'un précédent travail effectué par Robert L. Bogue (Blog: http://www.thorprojects.com/blog, Auteur du SharePoint Shepherd's Guide for End Users http://www.sharepointshepherd.com/)
Comme toujours, si vous avez des idées ou des corrections à apporter à ce code, n’hésitez pas à m’en faire part.
<Philippe/>