Showing posts with label LINQ. Show all posts
Showing posts with label LINQ. Show all posts

30 March 2010

Using LINQ to get filenames from a directory structure using multiple include and exclude patterns

This example shows how to get list of file names from a directory (including subdirectories) using multiple include- and exclude patterns.

The buildin method System.IO.Directory.GetFiles can handle only one include pattern and no exclude pattern and multiple patterns a definitely out of the question.

So I had to write a little helper method using LINQ to do the trick!

/// <summary>
/// Get the files from the specified path, including subdirectories, which match the specified pattern.
/// Note: To specified multiple patterns, separate them by a semicolumn (;)
/// </summary>
/// <param name="path">The path.</param>
/// <param name="includePatterns">An array of include patterns. Using wildcards.</param>
/// <param name="excludePatterns">An array of exclude patterns. Using Regular expressions.</param>
/// <returns>Returns an array of filenames.</returns>
public static string[] GetFiles(string path, string[] includePatterns, string[] excludePatterns)
{
 if (string.IsNullOrEmpty(path))
 {
  throw new ArgumentNullException("path", "Argument should not be NULL or an empty string.");
 }
 if (includePatterns == null)
 {
  includePatterns = new string[0];
 }
 if (excludePatterns == null)
 {
  excludePatterns = new string[0];
 }

 var files = from includePattern in includePatterns
    from includeFile in Directory.GetFiles(path, includePattern, SearchOption.AllDirectories)
    from excludePattern in excludePatterns.DefaultIfEmpty()
    where excludePattern == null || !Regex.IsMatch(includeFile, excludePattern, RegexOptions.IgnoreCase)
    select includeFile;

 return files.ToArray();
}

The following example gets all files that match the wildcard patterns 'DdH.*.Helpers.dll' & 'DdH.*.exe' from the c:\temp directory, but will exclude all files that match the regular expression 'Tests\.dll'.

string[] fileNames = GetFiles(@"c:\temp", new string[] { "DdH.Helpers.*.dll", "DdH.*.exe" }, new string[] { "Tests\.dll" });

26 April 2009

SPSiteColumnUsage, Find all references to a site column

When I try to delete a site column from a SharePoint site I often get the message "Site columns which are included in content types cannot be deleted. Remove all references to this site column prior to deleting it."

Well with only a few content types this isn't so hard to click through all content types and checking if the site column is used, but with a lot of them it's no fun and can be a hell of a job.

To make my life a bit easier I decided to create a piece of code that could delete site columns programmatically. I knew that SharePoint object model has a class that can return the usage of content types, namely SPContentTypeUsage. With this class you can tell if a content type is used in one or more lists somewhere inside the complete site collection and what the urls to those lists are.

My guess was that there should also be something like a site column usage, but I was wrong!

After some investigation on MSDN, several SharePoint blogs and good old Reflector, I created a SPSiteColumnUsage class. This class uses the LINQ and SPContentTypeUsage and can find all references to content types and lists where a site columns is in use.

SPSiteColumnUsage
Methods
  • GetUsages, this method takes a SPField instance as input argument and returns an array of SPSiteColumnUsage objects.
Properties
  • Id, the Id of the site column.
  • Scope, the scope of the site column.
  • ContentTypeId, the id of a content type where the site column is in use.
  • IsUrlToList, indicates if the information is about a list or a content type.
  • Url, the server relative to a list where the site column is in use.


Usage example to get info about the site column: Title
using (SPSite site = new SPSite("http://demosite"))
{
    using (SPWeb web = site.OpenWeb())
    {
        SPField field = web.Fields.GetFieldByInternalName("Title);

        Console.WriteLine("Field: {0}", fieldName);
        Console.WriteLine("==============================");

        var usage = SPSiteColumnUsage.GetUsages(field);
        foreach (var u in usage)
        {
            Console.WriteLine("Id: {0}", u.Id);
            Console.WriteLine("Scope: {0}", u.Scope);
            Console.WriteLine("ContentTypeId: {0}", u.ContentTypeId);
            Console.WriteLine("IsUrlToList: {0}", u.IsUrlToList);
            Console.WriteLine("Url: {0}", u.Url);
            Console.WriteLine();
        }
    }
}
See a working example on http://SPSiteColumnUsage.codeplex.com

The class also makes use of the extension method CoppyToArray I created.
For this method see a previous post: Using LINQ to query SharePoint collections

/// 
/// Class with site column usage information.
/// 
public class SPSiteColumnUsage
{
   public static IList< SPSiteColumnUsage> GetUsages(SPField field)
   {
       List< SPSiteColumnUsage> list = new List< SPSiteColumnUsage>();

       if(field != null && field.UsedInWebContentTypes)
       {
           // Use reflection to get the fields collection for the specified field
           SPFieldCollection fieldCollection = field.GetType().GetProperty("Fields", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(field, null) as SPFieldCollection;
           if(fieldCollection != null)
           {
               // Get the web context from the collection
               SPWeb web = fieldCollection.Web;

               // First collect all contenttypes to an array, so we can use Linq
               var contentTypes = web.ContentTypes.CopyToArray< SPContentType>();

               // Filter contenttypes where field is used
               var contentTypesWithField = (from contentType in contentTypes
                                            where contentType.Fields.ContainsField(field.InternalName)
                                            select contentType).ToArray();

               // Create usages for fields in contenttypes
               foreach(SPContentType contentType in contentTypesWithField)
               {
                   SPSiteColumnUsage siteColumnUsage = new SPSiteColumnUsage(field.Id, field.Scope, contentType.Id);
                   list.Add(siteColumnUsage);
               }

               // Get unique lists-urls where contentypes are beeing used
               var listUrlsContentTypeUsage = (from contentType in contentTypesWithField
                                               let contentTypeUsages = SPContentTypeUsage.GetUsages(contentType)
                                               from contentTypeUsage in contentTypeUsages
                                               where contentTypeUsage.IsUrlToList
                                               select contentTypeUsage.Url).Distinct().ToArray();

               // Create usages for fields in list
               foreach (string listUrl in listUrlsContentTypeUsage)
               {
                   SPSiteColumnUsage siteColumnUsage = new SPSiteColumnUsage(field.Id, field.Scope, listUrl);
                   list.Add(siteColumnUsage);
               }
           }
       }

       return list;
   }

   private readonly Guid _Id;
   private readonly string _Scope;
   private readonly SPContentTypeId _ContentTypeId;
   private readonly string _Url;

   private SPSiteColumnUsage(Guid id, string scope, SPContentTypeId contentTypeId)
   {
       _Id = id;
       _Scope = scope;
       _ContentTypeId = contentTypeId;
       _Url = null;
   }

   private SPSiteColumnUsage(Guid id, string scope, string url)
   {
       _Id = id;
       _Scope = scope;
       _ContentTypeId = SPContentTypeId.Empty;
       _Url = url;
   }

   public Guid Id
   {
       [DebuggerStepThrough]
       get { return _Id; }
   }

   public string Scope
   {
       [DebuggerStepThrough]
       get { return _Scope; }
   }

   public SPContentTypeId ContentTypeId
   {
       [DebuggerStepThrough]
       get { return _ContentTypeId; }
   }

   public string Url
   {
       [DebuggerStepThrough]
       get { return _Url; }
   }

   public bool IsUrlToList
   {
       [DebuggerStepThrough]
       get { return _ContentTypeId.Equals(SPContentTypeId.Empty) && !string.IsNullOrEmpty(_Url); }
   }
}

08 March 2009

Using LINQ to query SharePoint collections

I came across a challenge when I wanted to using LINQ on several SharePoint collection classes and I noticed that it wasn't possible to do this....So why not? LINQ lets you query any collection implementing the IEnumerable interface and after some research I found that most of the SharePoint collections don't implement this interface.

Ok, how to overcome this challenge? Arrays implement IEnumerable....Copy the items from the collection to an array! SharePoint collections implement the ICollection interface which provides the method: void CopyTo(Array array, int index), so this should not be a problem. Well it does!

SharePoint collections derive from a class called SPBaseCollection and this class implements ICollection. But because the explicit implementation of CopyTo, this method is private.

For this I wrote a little helper extension method that makes it possible to copy a SharePoint collections (or any class that implements ICollection) to an array of a particular type and validate for null at the same time.
public static class Helpers
{
 public static T[] CopyToArray(this ICollection collection)
 {
     if (collection == null) { return new T[0]; }

     T[] array = new T[collection.Count];
     collection.CopyTo(array, 0);

     return array;
 }
}
Now it is possible to make Linq queries like this:
using (SPSite site = new SPSite("http://demosite"))
{
 using (SPWeb web = site.OpenWeb())
 {
     // Get all webs ordered by Title
     var items1 = web.Webs.CopyToArray< SPWeb>().OrderBy(p => p.Title);
     foreach (var item in items1)
     {
         Console.WriteLine(item.Title);
     }

     // Get all content types grouped by group and ordered by group and name
     var items2 = web.ContentTypes.CopyToArray< SPContentType>().GroupBy(p => p.Group).OrderBy(g => g.Key);
     foreach (IGrouping<> group in items2)
     {
         Console.WriteLine(group.Key);

         foreach (var item in group.OrderBy(p => p.Name))
         {
             Console.WriteLine(item.Name);
         }
     }

     // Get a list by name and not by title
     var list = web.Lists.CopyToArray< SPList>().SingleOrDefault(p => p.RootFolder.Name.Equals("pages", StringComparison.InvariantCultureIgnoreCase));
     if (list != null)
     {
         Console.WriteLine("List: {0} found!", list.Title);
     }
 }
}