Monday, July 26, 2010

Provision publishing pages in a sandbox solution

This one turned out to be an interesting finding – at first I thought that I can just copy a regular “Module” feature from a farm solution and that it will work smoothly within a sandbox solution. For instance:

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

  <Module Name="MyPages" Path="MyPages" Url="Pages">

    <File Url="mypage.aspx" Path="mypage.aspx" Type="GhostableInLibrary">

      <Property Name="Title" Value="Home" />

      <Property Name="ContentType" Value="Welcome Page" />

      <Property Name="PublishingPageLayout" Value="~SiteCollection/_catalogs/masterpage/MyWelcome.aspx, My Welcome Page" />

      <AllUsersWebPart WebPartZoneID="BottomLeftZone" WebPartOrder="1">       

          <![CDATA[

<WebPart xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.microsoft.com/WebPart/v2">

  <Title>Search Box</Title>

  <FrameType>None</FrameType>

  <Description>Displays a search box that allows users to search for information.</Description>

  <MissingAssembly>Cannot import this Web Part.</MissingAssembly>

  <Assembly>Microsoft.Office.Server.Search, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>

  <TypeName>Microsoft.SharePoint.Portal.WebControls.SearchBoxEx</TypeName>

</WebPart>]]>       

      </AllUsersWebPart>

    </File>

  </Module>

</Elements>

As you see, this is a fairly simple “Module” elements file with a single “File” element that provisions a publishing page containing one web part to the standard “Pages” library. The “mypage.aspx” file is the standard template redirection one-liner page that you can take from any of the standard SharePoint publishing site definitions:

<%@ Page="" Inherits="Microsoft.SharePoint.Publishing.TemplateRedirectionPage,Microsoft.SharePoint.Publishing,Version=14.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" %><%@ Reference="" VirtualPath="~TemplatePageUrl" %><%@ Reference="" VirtualPath="~masterurl/custom.master" %>

So, when I added this feature to a sample sandbox solution of mine, deployed the latter and activated the feature I immediately noticed two quite unpleasant issues:

  • the first one was that the file was provisioned in a draft state (not checked in)
  • the second was even worse – the web part was missing on the page

I started wondering what may be causing these issues and thought that at least the first one was not that serious – after all with a little bit of code in a feature receiver it should be possible to publish the provisioned page or pages. But then thinking about the second issue I realized that it will require much more than several lines of code if I want to get that fixed with custom programming (and why should I get into that much trouble since this is supposed to be available out-of-the-box). The fact was that I already had the code that does the whole trick of parsing the “Module” elements file, parses a selected “File” element and adds the web parts to a web part page using the SharePoint object model – I had used that in my “web part manager” tool, though I wasn’t sure if the code (and it was several hundred lines of code) would work normally in a sandbox.

Reluctant to use this cumbersome “fix”, it occurred to me that I can try using the built-in SharePoint feature of saving a site as a template which in SharePoint 2010 creates a “wsp” package (which is a sandbox solution) that contains a “Module” feature provisioning all files (including publishing pages) in the site. My plan was simple – after I create the “wsp” package of my test site with publishing pages I will create a sub-site based on it and will check whether the provisioning of the publishing pages will fail in the same miserable way and if it doesn’t I will open the “wsp” file and inspect the XML of the “Module” element to see what does the trick.

So, as I suspected, the first part went smoothly which meant that the “save as template” feature worked perfectly with publishing pages although the “save site as template” command is missing in the “site settings” page of sites based on publishing site templates and you have to navigate to it manually - _layouts/savetmpl.aspx (it’s actually interesting to know why it is hidden in the first place, I can only suspect that there is a serious reason for that).

Then came the interesting part of checking the “Module” and “File” elements in the “Module” feature containing the publishing pages (actually I was testing with just one page) – first I reproduced the normal working of the feature by copying the elements without any changes and then started to remove parts, that I thought, weren’t necessary so that I could find out which is the critical part. First I checked the attributes of the “Module” and “File” elements but there was nothing extra or peculiar there (the “Level” attribute of the “File” element was not present), it wasn’t a “Property” element for the “_ModerationStatus” system column either. Then I saw a big chunk worth deleting – the “Property” element of the “MetaInfo” system column – it basically contains the concatenated values of all other fields and file property bag items of the provisioned file – and after I deleted it the feature started to behave in the exactly same way as my first feature. I had a closer look at the “MetaInfo” element and after some testing found out that it is this part that does the trick:

vti_setuppath:SR|SiteTemplates\\SPS\\Default.aspx

The meaning of this one was almost clear to me – instead of using the “aspx” file provided in the feature the publishing page should use a file already available in the subfolders of the “14” hive. Another thing that immediately occurred to me was that the “Module” element also accepts a “SetupPath” attribute, so I quickly rewrote my first feature so that it now looked like:

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

  <Module Name="MyPages" SetupPath="SiteTemplates\SPS" Url="Pages">

    <File Url="mypage.aspx" Path="default.aspx" Type="GhostableInLibrary">

      <Property Name="Title" Value="Home" />

      <Property Name="ContentType" Value="Welcome Page" />

      <Property Name="PublishingPageLayout" Value="~SiteCollection/_catalogs/masterpage/MyWelcome.aspx, My Welcome Page" />

      <AllUsersWebPart WebPartZoneID="BottomLeftZone" WebPartOrder="1">       

          <![CDATA[

<WebPart xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.microsoft.com/WebPart/v2">

  <Title>Search Box</Title>

  <FrameType>None</FrameType>

  <Description>Displays a search box that allows users to search for information.</Description>

  <MissingAssembly>Cannot import this Web Part.</MissingAssembly>

  <Assembly>Microsoft.Office.Server.Search, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>

  <TypeName>Microsoft.SharePoint.Portal.WebControls.SearchBoxEx</TypeName>

</WebPart>]]>       

      </AllUsersWebPart>

    </File>

  </Module>

</Elements>

So, as you see there are only two small changes in the attributes of the “Module” and “File” attributes:

  • the first one is the adding of the “SetupPath” attribute to the “Module” element and removing the existing “Path” attribute
  • the second one is the changing of the value of the “Path” attribute of the “File” element to “default.aspx”

The idea of these two attributes is that under this path “14\Template\SiteTemplates\SPS\default.aspx” there is a standard SharePoint file which is exactly a template redirection page, exactly the same as the one that I showed above. This means that instead of the “aspx” file in the feature (which can be removed from the feature actually) an existing standard SharePoint file will be used for the provisioning of the publishing page. The other positive thing is that the cumbersome “MetaInfo” property is also not necessary since the “SetupPath” attribute of the “Module” element achieves the same result much more elegantly.

And several words as a small conclusion – though the syntax of the standard SharePoint feature elements seem the same there’re certain differences in the way the SharePoint artifacts get provisioned in farm and sandbox solutions. So, I guess, we can expect similar smaller or bigger surprises with the new SharePoint 2010 and especially with the new sandbox solutions.

Wednesday, July 7, 2010

LINQ to SharePoint cross list query

In SharePoint 2010 we already have the LINQ to SharePoint support that is a great improvement over the traditional SPListItem + CAML approach for exposing and modifying SharePoint list data. The built-in LINQ to SharePoint implementation is a complete ORM framework which provides full support for CRUD operations.

But in SharePoint there is one other method for retrieving list data – this is the cross list query support that is available through the SPWeb.GetSiteData method. The cross list query provides means to fetch list data from many lists in the entire site collection simultaneously and can be useful in many scenarios; it is also internally used by the standard Content Query web part (CQWP). Still there is no out-of-the-box support for LINQ to cross list query in SharePoint 2010, so I decided to create a small framework with a light-weight and simple implementation of a LINQ query provider for cross list query CAML. Note here that this solution is focused mainly on the lambda expression to CAML translation and doesn’t use entities that are mapped to SharePoint lists or content types – in this sense it is not an ORM system (you will see that from the demonstration below). Another important note – since the object model support for the cross list query hasn’t changed much in SharePoint 2010, this solution works in both SharePoint 2007 and SharePoint 2010. You can download the code of the custom LINQ framework from here.

And now, let’s jump directly to a small code sample:

using (SPSite site = new SPSite("http://testserver/sites/testsite"))

{

    SPWeb web = site.OpenWeb();

    IQueryable<DataRow> result = web.GetSiteData()

           .Webs(WebScope.SiteCollection)

           .Lists(BaseListType.GenericList)

           .ViewFields(Fields.ID, Fields.Text1.Nullable, Fields.Date1.Nullable, Fields.Bool1.Nullable, Fields.Look1.Nullable, Fields.Numb1.Nullable)

           .CamlWhere(r =>

                Fields.Text1.Value.Contains ("test")

                && (Fields.Look1.LookupId == 1 || Fields.Look1.Value == "test")

                && (Fields.Bool1.Value != true || Fields.Date1.Value > DateTime.Today.AddDays(-2))

            )

           .CamlOrderBy(Fields.Date1.Descending, Fields.Text1)

           .RowLimit(10);

 

    foreach (DataRow row in result)

    {

        Console.WriteLine(Fields.ID[row]);

        Console.WriteLine(Fields.Text1[row]);

        Console.WriteLine(Fields.Numb1[row]);

        Console.WriteLine(Fields.Bool1[row]);

        if (Fields.Look1[row] != null)

            Console.WriteLine(Fields.Look1[row].LookupValue);

    }

}

As you can see the code snippet starts with opening an SPWeb instance and then there is a sequence of extension method calls the end result of which is an IQueryable of DataRow. So there are several important things to note here before going into detail:

  • unlike the standard LINQ to SharePoint implementation there are no entity classes here, so instead of IQueryable<SomeEntity> the custom query provider returns always IQueryable<DataRow> (keeping things much closer to the SPWeb.GetSiteData method which returns a DataTable)
  • the standard IQueryable extension methods Select, Where, First, FirstOrDefault, etc are not used and are actually not supported. Instead there is a small set of custom extension methods which map directly to the properties of the SharePoint SPSiteDataQuery class (I’ll explain how these can be used below).
  • instead of entity classes that you normally generate with the SPMetal utility, a static class is used here (class Fields) which exposes a number of static variables that represent SharePoint site and list fields. A small wrapper class is used for these static variables (whose name is, you guessed it - Field) and it is used in all cases that in the CAML you would use the “FieldRef” element. So this is actually the greatest difference between the standard LINQ to SharePoint and this custom implementation – there are no wrapping classes on the entity level (content type or list level in SharePoint terms) but instead – on the column/field level. This has the drawback of not having your data neatly packed into dedicated classes but on the other hand can give you more flexibility over the static wrappers in terms of the ease with which you can modify the meta-data (the fields that you want to retrieve or use to filter or sort on) of your queries without it being necessary to re-generate the entity classes every time.

And here is the definition of the sample “Fields” class that is used in the first code snippet:

public static class Fields

{

    public static readonly Field<int, int> ID =

        new Field<int, int>("ID", "Integer", SimpleCamlValueConvertor.Instance);

 

    public static readonly Field<string, string> Text1 =

        new Field<string, string>("Text1", "TEXT", SimpleCamlValueConvertor.Instance);

 

    public static readonly DateField<DateTime, DateTime> Date1 =

        new DateField<DateTime, DateTime>("Date1", "DateTime", SimpleCamlValueConvertor.Instance);

 

    public static readonly Field<double, double> Numb1 =

        new Field<double, double>("Numb1", "Number", SimpleCamlValueConvertor.Instance);

 

    public static readonly Field<bool, bool> Bool1 =

        new Field<bool, bool>("Bool1", "Boolean", SimpleCamlValueConvertor.Instance);

 

    public static readonly LookupField<string, SPFieldLookupValue> Look1 =

        new LookupField<string, SPFieldLookupValue>("Look1", "Lookup", SimpleCamlValueConvertor.Instance);

}

So, this is basically sorts of container or placeholder class – it can be named with any suitable name and the important part is the set of static member variables (these can be defined as member properties as well) that represent the SharePoint fields that you want to use in your site data queries. The type of the member variables is the generic Field<TValue, TParsedValue> class, which as I mentioned is used as a small wrapping class for SharePoint list and/or site fields. You can also see in the code sample that two other generic types are used – DateField and LookupField – they actually inherit the Field class (these two are the only inheritors) and provide specific support for the SharePoint date and lookup field types. At this point I haven’t implemented a utility that automatically generates the container class with the mapped “Field” member variables but as you can see it is pretty easy to create one very quickly by hand – it is basically one line of code per field.

And this is the declaration of the generic Field class (this is only the declaration of the class as it appears in the meta-data code view in Visual Studio):

public class Field<TValue, TParsedValue> : Field

{

    public Field(string name, string type, ICamlValueConvertor convertor);

    public TValue Value { get; }

    public virtual TParsedValue this[DataRow item] { get; }

}

as you see, it inherits a non-generic class also named “Field” (which is also an abstract class):

public abstract class Field

{

    public Field(string name, string type, ICamlValueConvertor convertor);

    public Field Descending { get; }

    public Field Nullable { get; }

}

The two classes are very simple with just a couple of properties, a constructor and a custom indexer. As for the separation in two classes – a base one and an inheritor (which is also a generic class) – you will see the logic behind this design solution in a while.

So, let me now give you a brief explanation about the members of the “Field” classes:

  • first – the constructor – it takes two string parameters in which you should provide the name (the internal name actually) and CAML type (as it appears in the Type attribute of the CAML “Value” element) of the SharePoint field, and a third parameter of type ICamlValueConverter – this is a custom interface type defined in this custom LINQ framework. You don’t have to know details about this interface, since there is already a class that implements it which you can use directly (I will briefly give you some details about the interface below). And here is a sample usage:

    new Field<string, string>("Text1", "TEXT", SimpleCamlValueConvertor.Instance);

    the field in this case is a single line text field - “Text1” is its internal name and “TEXT” is the CAML type that should be used for it. For the third parameter you can see that the SimpleCamlValueConvertor class is used (this is a singleton class, available through its “Instance” static property).
  • the generic “Value” property – this property has one purpose only – it should be used only in the custom CamlWhere extension method, inside its lambda expression parameter. It is this expression tree actually that will be translated to the CAML “Where” element. If you have for example this comparison expression with the Value property:


    Fields
    .Bool1.Value != true

    it will get translated to this CAML:

    <Neq>

      <FieldRef Name="Bool1" />

      <Value Type="Boolean">1</Value>

    </Neq>

    The exact .NET type of the “Value” property is determined by the first generic type parameter of the Field<TValue, TParsedValue> class. Basically only five .NET types can be used for it – string, bool, int, double and DateTime, depending on the underlying SharePoint field type. For all SharePoint field types whose field value type is not a primitive .NET type, the System.String type should be used – including the lookup, multi lookup, multi column, multi choice, etc. field types – this is because of the limitations of the CAML syntax which doesn’t allow more complex comparison operations.
  • the generic indexer that can be used to extract typed values from the DataRow instances returned when the IQueryable gets enumerated (see above in the first code snippet). The exact .NET type of the return value is determined by the second generic type parameter of the Field<TValue, TParsedValue> class. You can provide the corresponding field value type here which depending on the field type of the underlying SharePoint field can be SPFieldLookupValue, SPFieldLookupValueCollection, SPFieldMultiColumnValue, etc, as well as just a simple .NET value type like int, double, bool. The converting of the DataRow column value to the exact .NET type is handled normally by the SimpleCamlValueConvertor class, which you provide as a parameter to the Field class constructor. You can use a custom class here too that implements the ICamlValueConvertor interface if you want to have some different converting logic. Note that the SimpleCamlValueConvertor class doesn’t support “nullable” types like Nullable<int>, Nullable<double>, etc (or with the short-hand notation: int?, double?, etc), and for fields whose value type is a .NET value type when the underlying list item contains a null value in the corresponding column, the indexer will return the default value for that .NET value type (DateTime.MinValue for System.DateTime, false for System.Boolean, 0 for System.Int32 and 0.0 for System.Double).
  • “Descending” property – this property whose type is the abstract Field class should be used in the custom CamlOrderBy extension method only:

    CamlOrderBy(Fields.Date1.Descending, Fields.Text1)


    the idea is that when you provide only a Field member variable only (like Fields.Text1) the sorting will be on that column ascending and if you use the “Descending” property of the Field member variable (Fields.Date1.Descending) then the sorting will be descending. The above method call will be translated into this CAML:

    <OrderBy>

      <FieldRef Name="Date1" Ascending="FALSE" />

      <FieldRef Name="Text1" Ascending="TRUE" />

    </OrderBy>

  • “Nullable” property – this is similar to the “Descending” property, its type is again the abstract Field class and it is intended for use only in the “ViewFields” custom extension method:

    ViewFields(Fields.ID, Fields.Text1.Nullable);


    and this method call will be translated to this CAML:

    <ViewFields>

      <FieldRef Name="ID" Nullable="FALSE" />

      <FieldRef Name="Text1" Nullable="TRUE" />

    </ViewFields>

And a quick look at the LookupField and DateField classes which inherit the Field<TValue, TParsedValue> class and each of which adds one extra member property:

  • the “LookupId” property of the LookupField class – this is an integer property that can be used only in the lambda expression parameter of the custom CamlWhere extension method (like the “Value” member property of the Field class):

    .CamlWhere(r =>

       Fields.Look1.LookupId == 1 || Fields.Look1.Value == "test")

    which will translate to:

    <Where>

      <Or>

        <Eq>

          <FieldRef Name="Look1" LookupId="TRUE" />

          <Value Type="Lookup">1</Value>

        </Eq>

        <Eq>

          <FieldRef Name="Look1" />

          <Value Type="Lookup">test</Value>

        </Eq>

      </Or>

    </Where>

    as you can see, you can use both the “Value” and the “LookupId” properties of the LookupField class inside the lambda expression but they have different semantics – when you use the “LookupId” property the generated CAML contains the extra “LookupId” attribute in the “FieldRef” element. This means that when you use the “Value” property the comparison will be on the value of the lookup field (show field) in the lookup list and when you use the “LookupId” property the comparison will be on the ID of the lookup list item (the referenced item in the lookup list).
  • the “DateValue” property of the DateField class – this is again a property that can be used only in the lambda expression parameter of the custom CamlWhere extension method. The difference between the “Value” and “DateValue” properties of the DateField class is that when you use the “Value” property the comparison will be on the full date-time value, while for the “DateValue” property the comparison will be on the date part only of the DateTime argument (the CAML will contain the extra “IncludeTimeValue” attribute when you use the “Value” property).

And here are the declarations of the custom extension methods:

public static class SPWebExtensions

{

    public static IQueryable<DataRow> GetSiteData(this Microsoft.SharePoint.SPWeb web);

}

public static class SiteDataExtensions

{

    public static IQueryable<DataRow> CamlOrderBy(this IQueryable<DataRow> items, params Field[] fields);

    public static IQueryable<DataRow> CamlWhere(this IQueryable<DataRow> source, Expression<Func<DataRow, bool>> predicate);

    public static IQueryable<DataRow> Lists(this IQueryable<DataRow> items, BaseListType listType);

    public static IQueryable<DataRow> Lists(this IQueryable<DataRow> items, params Guid[] listIDs);

    public static IQueryable<DataRow> Lists(this IQueryable<DataRow> items, string serverTemplate);

    public static IQueryable<DataRow> Lists(this IQueryable<DataRow> items, BaseListType listType, bool hidden, int maxListLimit);

    public static IQueryable<DataRow> Lists(this IQueryable<DataRow> items, bool hidden, int maxListLimit, params Guid[] listIDs);

    public static IQueryable<DataRow> Lists(this IQueryable<DataRow> items, string serverTemplate, bool hidden, int maxListLimit);

    public static IQueryable<DataRow> RowLimit(this IQueryable<DataRow> items, uint rowLimit);

    public static IQueryable<DataRow> ViewFields(this IQueryable<DataRow> items, params Field[] viewFields);

    public static IQueryable<DataRow> Webs(this IQueryable<DataRow> items, WebScope webScope);

}

  • SPWebExtensions.GetSiteData – this is the method that you will use first to get a reference of IQueryable<DateRow> that you can then use with the extension methods defined in the SiteDataExtensions class. With the standard SharePoint 2010 LINQ support we have the “context” class which exposes public properties that return IQueryable for the different entities that you have generated. And with this custom LINQ implementation you see that it is much simpler – just one extension method that you use on a SPWeb instance. And actually this method has the same name as the standard SPWeb.GetSiteData method (which accepts a SPSiteDataQuery parameter), but this one is parameterless. After you have the IQueryable<DateRow> instance you can then call the extension methods defined in SiteDataExtension which directly map to the properties of the standard SPSiteDataQuery class, but are much easier to use, because you don’t have to use the cumbersome CAML syntax:
  • SiteDataExtensions.CamlOrderBy – this method generates the “OrderBy” element of the CAML that is provided to the SPSiteDataQuery.Query property. It accepts a “params” array parameter of the abstract “Field” class which you can provide as “Field” static member variables of your “container” class (class Fields in the sample above) or their “Descending” properties if you want to use descending sorting (for sample code see the paragraph about the Field.Descending property).
  • SiteDataExtensions.CamlWhere - this method generates the “Where” element of the CAML that is provided to the SPSiteDataQuery.Query property. Note that it has the same notation as the standard Queryable.Where extension method (I could have just as well used the latter instead, but with all other custom extension methods, I decided to have all of these custom). It is actually the expression tree that the .NET framework generates from its lambda expression parameter that gets translated to the “Where” CAML. Note that since the resultant CAML is with very limited comparison expression capabilities the custom Queryable provider of this implementation supports very few .NET methods and operators:
    • the binary operators “&&” (AndAlso) and “||” (OrElse) (that map to CAML “And” and “Or”)
    • the binary comparison operators “==” (Equal), “!=” (NotEqual), “>” (GreaterThan), “>=” (GreaterThanOrEqual), “<” (LessThan), “<=” (LessThanOrEqual) (these map to CAML “Eq”, “Neq”, “Gt”, “Geq”, “Lt”, “Leq” respectively, for equality/inequality to null: “== null” and “!= null” the corresponding CAML elements are “IsNull” and “IsNotNull”)
    • .NET methods – only two methods are supported – String.StartsWith and String.Contains (these map to CAML “BeginsWith” and “Contains”):

      Fields.Text1.Value.Contains ("test")

    • No other operators and methods are supported (the unary negation (unary “!”) is not supported either)
    Note also the following restrictions for the expression tree from the lambda expression of the CamlWhere method:
    • one of the operands of the binary comparison operators (no matter whether left or right operand) should always be a “Field” static member variable/property of your container class (class “Fields” from the sample) using its “Value”, “LookupId” or “DateValue” properties (see the sample code above)
    • the other operand of the binary comparison operators should be a constant expression or an expression that doesn’t reference “Field” member variables/properties and that evaluates to the same .NET type as the “Value” property of the “Field” instance from the other operand (note that it should evaluate to the exactly same .NET type otherwise you have to explicitly use type casting for that operand). Note also that you can use null equality/inequality comparison even for .NET value types (e.g. int, double, DateTime) – these will be translated to CAML “IsNull” and “IsNotNull”:

      Fields.Date1.Value == null

      The operand can contain any type of expressions and methods – the restrictions mentioned above for the allowed operators/methods don’t apply here, for instance you can have some more complex calculations and/or call other custom methods:

      Fields.Date1.Value > DateTime.Today.AddDays(-2)

    • For “Field” members with Boolean “Value” property you should explicitly use the equality/inequality operator (even though it may seem more economical to omit it):

      Fields.Bool1.Value == true

    It is important to note here that if you violate one of the above mentioned restrictions you will get various exceptions (of the custom ParseLinqException type) during run-time. Note also that the delegate type of the lambda expression parameter accepts a DataRow parameter but this one is not used (and shouldn’t be used) anywhere in the lambda expression’s body.
  • SiteDataExtensions.Lists – this method generates the CAML that is provided to the SPSiteDataQuery.Lists property. The method comes with six overloads that provide handy support for the different list modes of the cross list query – either using the base list type, or the list server template or providing an array of SharePoint list IDs.
  • SiteDataExtensions.RowLimit – this methods maps to the RowLimit property of the SPSiteDataQuery – it accepts an unsigned integer parameter with which you specify how many list items the query should return.
  • SiteDataExtensions.ViewFields – this method generates the CAML that is provided to the SPSiteDataQuery.ViewFields property. It accepts a “params” array parameter of the abstract “Field” class which you can provide as “Field” static member variables of your “container” class (class Fields in the sample above) or their “Nullable” properties if you want the generated “FieldRef” elements to have the “Nullable” attribute added (see the first code sample).
  • SiteDataExtensions.Webs – this method generates the CAML that is provided to the SPSiteDataQuery.Webs property.

Quick start with the custom LINQ framework:

And lastly several words of how to use the custom LINQ framework inside your SharePoint projects.
The solution comes in a single assembly - Stefan.SiteData.Linq (you can use it as a separate assembly or to incorporate the code in a custom assembly of yours). The steps to use it are as follows:

  1. If you use it as a separate assembly you need to have an assembly reference of it in your SharePoint project.
  2. You then need to create the static “container” class and create a static member variable (or property) for every SharePoint field that you will use in your cross list queries (“FieldRef” elements inside “ViewFields”, “Where” and “OrderBy” CAML elements) – these should be of the Field<TValue, TParsedValue>, LookupField<TValue, TParsedValue> or DateField<TValue, TParsedValue> classes type (check the definition of the sample “Fields” class above).
  3. You can proceed with your list data retrieving code – after you have an SPWeb instance, you can start with calling the extension SPWebExtensions.GetSiteData extension method to create the IQueryable<DataRow> and then start calling the extension methods of the SiteDataExtensions class so that you specify custom filtering, sorting and view fields that should be included in the result-set (check the first code snippet at the top).
  4. Lastly when you start iterating the IQueryable<DataRow> you can make use of the “Field” class indexer so that you get typed values from your SharePoint fields (you can as well directly access the values from the DataRow instances and use them as simple strings). As I mentioned above the conversion logic for the DataRow column string value to specific SharePoint field value .NET types is in the custom SimpleCamlValueConvertor class (you can check its code, it is really simple). You can provide your custom conversion implementation if you create a class implementing the ICamlValueConverter interface:

        public interface ICamlValueConvertor

        {

            string ConvertToString<T>(T value);

            T ConvertFromString<T>(string value);

        }

    It is the ConvertFromString generic method that is used for the conversion and its generic parameter determines the return type of the method (this is also the .NET type of the indexer of the “Field” class). You can also inherit the SimpleCamlValueConvertor class and override its ConvertFromString method (it is defined as a virtual method) – then you won’t have to implement the ICamlValueConvertor.ConvertToString method.
  5. When you debug your code you will notice that when you hover over the IQueryable<DataRow> variable or add a debug watch for it you will be able to see the full query CAML generated by the custom IQueryable provider which can be very handy for issue troubleshooting.

Saturday, June 19, 2010

SharePoint 2010 – BinarySerializedWebPart element and XsltListViewWebPart provisioning

The BinarySerializedWebPart is a new feature element introduced in SharePoint 2010 and … its purpose is to be used mainly internally by the SharePoint save-site-as-template functionality. The element can appear below a View element in a Module feature that provisions web part pages with XLV web parts.

A known issue in SharePoint 2007 was that when you use the View element in an ONET.XML file or a Module feature element to add a List View web part (LVWP) to a page you can only specify one of the existing Views defined in the list definition feature (its schema.xml) of the target list instance – this is achieved with the BaseViewID attribute of the View element. So if you need a slightly modified View for a LVWP you have to either create a custom list definition (if you already have one – you have to extend it) or to use code to make the necessary list view schema changes – in the case of LVWP and XLV web parts there is always a hidden SPView associated with the web part (you can check a previous posting of mine treating this topic in detail - ListViewWebPart & SPView – two sides of the same coin). The problem with both approaches is that they require some extra effort for this relatively simple provisioning task.

And how about the new SharePoint 2010 – well, the View element works pretty much the same in the sense that if you use the View element alone you have again only the BaseViewID attribute to specify a View definition (limited to the predefined ones in the list template’s schema.xml). And here comes the new BinarySerializedWebPart element – you can see it working when you, say, have a normal team site and add a XLV web part to its home page and then modify the View of the web part adding for instance several more view fields and changing the filter fields. Then you can use the “save site as template” command from the “site settings” page of the site and after you have the site template, you can create a new site based on it, where you will see that you have the exactly same XLV web part with the exact same customizations in place (this didn’t work in the SharePoint 2010 beta but is fixed in the RTM version). The last exercise may sound like a nonsense since we had this functionality in SharePoint 2007 too, but the important difference here is that site templates in SharePoint 2007 were saved as “stp” files while in SharePoint 2010 site templates are saved as standard “wsp” solution packages comprising standard SharePoint features. And this means that in SharePoint 2010 it is possible to provision XLV web parts with arbitrary view customizations totally declaratively with standard feature element syntax. You can check that when you open the “wsp” file and extract the feature files from it using a standard archiving program that can handle CAB files (“wsp” packages are actually CAB files). In the Modules feature of the “wsp” package (there is one per site template package) you will see a View definition that will look something like:

<View List="Shared Documents" DisplayName="" Url="" DefaultView="FALSE" BaseViewID="1" Type="HTML" WebPartOrder="0" WebPartZoneID="Left" ContentTypeID="0x" ID="g_ba709f71_6af5_4e3c_a8b1_01be2d3f95e8" Hidden="TRUE">

  <BinarySerializedWebPart>

    <GUIDMap>

      <GUID Id="5ba54c0a_6d0a_4667_8889_27fcfc904193" ListUrl="Lists/Announcements" />

      <GUID Id="2d9a46bd_f3e1_4cc6_9630_5763fc589317" ListUrl="Lists/Links" />

      <GUID Id="76f78fed_a0f9_498b_b2e4_8aa9ff2b4a2f" ListUrl="Shared Documents" />

      <GUID Id="ea419abd_36c7_4a42_94e3_b661ef52af44" ListUrl="Lists/Calendar" />

    </GUIDMap>

    <WebPart ID="{ba709f71-6af5-4e3c-a8b1-01be2d3f95e8}" WebPartIdProperty="" List="{$ListId:Shared Documents;}" Type="1" Flags="8388621" DisplayName="" Version="1" Url="/sites/1/default.aspx" WebPartOrder="0" WebPartZoneID="Left" IsIncluded="True" FrameState="0" WPTypeId="{874f5460-71f9-fecc-e894-e7e858d9713e}" SolutionId="{00000000-0000-0000-0000-000000000000}" Assembly="" Class="" Src="" AllUsers="B6Dt/i4AAAABAAAAAAAAAAEAAAAvX2xheW91dHMvaW1hZ2VzL2l0ZGwucG5nAP8BFCsAEAICAgMCAgEEAAIBAgkBAAACCAKCAQUZL19sYXlvdXRzL2ltYWdlcy9pdGRsLnBuZwKVAQUmezc2Rjc4RkVELUEwRjktNDk4Qi1CMkU0LThBQTlGRjJCNEEyRn0FBkxpc3RJZCgpWFN5c3RlbS5HdWlkLCBtc2NvcmxpYiwgVmVyc2lvbj0yLjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkkNzZmNzhmZWQtYTBmOS00OThiLWIyZTQtOGFhOWZmMmI0YTJmBRVJbml0aWFsQXN5bmNEYXRhRmV0Y2ho" View="qKkwMQwAAAByAQAAeJxtkNsKwjAMhl+l9AUmeNsNPA0GHuf0vtp0BNtFYkX29tZ5QNiuQvL9P/kTdUR45AjO3DLV1RKsWGsPqZzTuThTI5MeWWJzydFBE7sBvCKDFsEMoIXBQDwAZgw6vC3Jf6bdHbjN1IYN8LTt2Yp55/jh5KOf1DVDrQNScxNH7e5Ru7H2JS7psUSPQWx1DSaVVXlYyGw8UsmXZGpFp3ieeJcigO/mqRzLz2yP/urgF7T/korInTSLqr3G1TkDWGIfyRMdinz/" />

  </BinarySerializedWebPart>

</View>

OK … so this is the fully declarative provisioning of the customized XLV web part but as you see what you have is actually the binary serialized property data of the web part which cannot in any way be manually modified in case you need some further adjustments. If you want to make some changes you need to have the feature deployed, change the provisioned XLV view and then export the whole containing site as a template again. Obviously this approach is good for the “saving as site template and site replication based on a site template” functionality but not that good for creating and maintaining manual view provisioning features. So again maybe the two previously mentioned approaches that were used for SharePoint 2007 before to get the XLV views configured correctly still can be considered as good alternatives.

And lastly one small detail – the BinarySerializedWebPart element will appear in the “save as site template” generated “wsp” solution package only for XLV web parts whose views were modified and are different from the standard predefined list template base views. This also holds for changes made to the XLV web part’s XSLT that you can do using the SharePoint 2010 designer. For XLV web parts that were not modified after they were added to a page, the “save as site template” command will generate a “standard” View element that will contain a normal CDATA section containing a WebPart element with the XML of all properties of the web part – i.e. the standard web part feature provisioning format that we use in web part features.

Sunday, May 16, 2010

SharePoint 2010 explorer (using the Client object model)

This is a small WinForm tool again using the TreeView-PropertyGrid combo very much like the popular SharePoint Manager 2010 tool (which became popular with its 2007 version), but instead using the client object model which was introduced in SharePoint 2010. This is actually the second tool in a row using the TreeView-PropertyGrid setup that I release – the first one being the WebPart manager tool, that I demonstrated several weeks ago – check here (both tools actually reuse the same core visual components).

The tool can be downloaded from here (sources).

spx1

So as you can see from the screen-shot the tool shows the client object model hierarchy in its TreeView control in the left pane and when an object is selected there its properties are displayed in the PropertyGrid control in the right. Since the client object model is used, only the hierarchy below the site collection level is displayed (as opposed to the “server” object model where you have everything starting with the farm level). On the other hand the client object model which utilizes internally the SharePoint web services  allows you to connect remotely to multiple SharePoint farms as long as you have the appropriate access rights.

The tool uses extensively – first and foremost reflection – the object model hierarchy in the TreeView is actually a one-to-one mapping with the real object model hierarchy since the child nodes are actually the real member properties of the class of the parent object. Secondly – the available commands/actions for every object in the hierarchy are actually the class methods of that object which are retrieved and eventually executed with reflection. The other thing used extensively in the tool is the PropertyGrid control itself – it is used not only to visualize the properties of the currently selected object but also in the execute action dialog where it gets populated with and allows the setting of the parameters of the underlying client object class method.

System requirements and installation notes: you need .NET 3.5 and SharePoint 2010 on the machine that you use the tool on. Note that the tool uses only two SharePoint assemblies – Microsoft.SharePoint.Client and Microsoft.SharePoint.Client.Runtime – so you will be able to run the tool on a non-SharePoint machine if you have these two copied on that machine – I am not sure though whether Microsoft allow these to be distributed in this way. The tool consists of 4 files: SPx.exe, SPx.Core.dll, SPx.SharePoint.Client.dll and spxclient.xml – there’re no special installation steps, you can just copy the files to the target machine and start the executable.

General usability notes: here is a small list of some useful notes and tips for using the tool:

  • the tool’s PropertyGrid is an extended version of the original PropertyGrid control. One very useful addition to it is the ability to edit multi-line text in string properties – you can use that when you right click a grid item containing a string property of the selected object – then a small dialog box will open in which the text value of the property will appear in a multi-line text box. This is handy especially in cases when you want to edit the Field.SchemaXml or the View.ViewQuery properties.

    spx6
  • when you select a node in the TreeView the tool calls the ClientContext.Load and ClientContext.ExecuteQuery methods to load the corresponding object and its properties. Various errors can occur during loading and these are displayed in a small strip just below the PropertyGrid control: 

    spx2 
    Most notifications reflect normal error conditions in the client object model – for example the Web.AssociatedMemberGroup property may be null in the “server” object model so when you try to load it with the client object model the latter will raise an exception to that effect. Another common notification is the “Info: Not all properties are available.” one – you will see it for all Folder objects which represent non-list folders (e.g. Web.RootFolder) – in this case the Folder.ConentTypeOrder and Folder.UniqueConentTypeOrder properties are invalid in the “server” object model and consequently cannot be retrieved using the client model.
  • when you right click a node in the TreeView control a context menu with all public instance methods of the object’s class will appear (the same menu for the currently selected object is available in the toolbar below the “Actions” button):

    spx3 
    when you click on one of the “reflected” method commands a dialog box will pop up – in the dialog a PropertyGrid control will be populated with the selected method’s parameters which you can use to provide appropriate values to execute the method:

    spx4 
  • a BIG NOTE here – though all public instance methods of the selected object are available in the context menu in many cases you won’t be able to execute the selected client object method – there’re two main reasons for that – 1st – the limitations of the UI of the tool – you may simply not be able to provide values in the PropertyGrid control for the method’s parameters – note that you can insert values in the grid items only for primitive types and not for class types. Secondly – there’re cases when you need to call several client object methods consecutively (and pass the returned objects from the preceding to the next method calls) before committing them with ClientContext.ExecuteQuery – the tool normally calls the ClientContext.ExecuteQuery method immediately after you click the “OK” button in the parameters dialog, so generally you won’t be able handle more complex scenarios with several method invocations (the tool though has a switch that can be used to disable temporarily the automatic call of ClientContext.ExecuteQuery – see below).
  • despite the above mentioned limitations you can use the tool in at least these cases: create, update and delete operations for Web, List, Field, View and Folder objects; upload, delete, check in/out and publish files; add and delete content types to lists; etc.
  • the tool provides two enhancements for calling methods containing parameters of types inheriting Microsoft.SharePoint.Client.ClientObject  and Microsoft.SharePoint.ClientValueObject. In the first case you have the option to click the grid item ellipses button which will display a small popup with the same object hierarchy tree that you have in the tool’s right pane from which you will be able to select an existing in the hierarchy ClientObject that can be used in the method’s parameter:

    spx5 
    as you see in the screen-shot this is very useful when you call the List.ContentTypes.AddExistingContentType method and provide a reference to a site content type so that you can add that content type to the list.
    In the second case the tool “flattens” the parameter and instead of a single grid item for the object it displays separate grid items for every property of the ClientValueObject inheriting class. A small example for that – when you call the WebCollection.Add method to create a new sub-web, in the method’s parameters PropertyGrid instead of a single grid item containing a null WebCreationInformation object, you will see six grid items containing the properties of the WebCreationInformation class – Url, Title, Description, Language, WebTemplate and UseSamePermissionsAsParentSite.
  • the “Add return object to tree” option in the method invocation dialog – this is available for all non-void methods and allows you to dynamically add the object returned from the method call to the hierarchy tree in the right pane – the object will appear as a child node of the currently selected node (all “dynamic” nodes will disappear if you refresh the parent or any of the ancestor nodes). In most cases you won’t need to use this option since most “get” methods will return an object that you already have in the client collection object and most “add” methods will return an object which will automatically appear in the client collection (the tool will internally refresh the collection object). There’re several cases however in which this is a very handy option and the only way to get certain objects – for instance List.GetItems, List.GetItemById, File.GetLimitedWebPartManager, etc.

Commands overview:

The tool provides a pretty simple command user interface – a toolbar at the top of the main window with three menu buttons:

  • Actions menu – this is context dependant – it displays the available commands/actions for the currently selected object. You have the “Refresh” command at the top, available for all nodes and below it commands for the available public instance methods of the object’s class (see above in the “General usability notes” section)
  • Connections menu – you have two commands here: “Connect …” and “Disconnect”. The former displays a dialog in which you should provide the URL to the local or remote site collection that should be opened in the tool. You can optionally provide user credentials and authentication mode here. You can open many different site collections from different SharePoint farms. The disconnect command removes the site collection of the currently selected node from the TreeView.
  • Settings menu – you have three commands here that operate as switches – you can check and uncheck them:
    • “Add method return object to tree” – when this is checked the check box switch with the same label in the method call dialog will be checked by default. Also handy for methods without parameters for which the method invocation dialog doesn’t get launched.
    • “Suppress display object in property grid” – this will effectively disable the PropertyGrid in the left pane, the selected object won’t be displayed there. This can be handy in some advanced situations when the very querying of the object’s properties may affect its state.
    • “Suppress ExecuteQuery” – also handy for some advanced situations. Note that the tool calls ClientContext.ExecuteQuery after each node selection in the TreeView and after each method call selected from the context menu. If you use this option to disable temporarily the ExecuteQuery calls you will be able to execute several client object methods consecutively and only after that commit them with ClientContext.ExecuteQuery. Note that the objects that you use for these calls should have been loaded in advance otherwise you will receive various “not initialized” errors for collections, objects and properties. Basically you should have a very good understanding of the client object model to be able to play with this option and overall there are not that many scenarios in which it can be helpful.

Thursday, May 6, 2010

How to display all versions in a SharePoint list view page – part 2

Several months ago I wrote a small posting describing how you can trick the standard list view web part to display all versions of the list items in a SharePoint list – link. One problem though with this was that even if the list view columns display the values of the relevant item versions the “Name” column in document libraries links always to the current version of document (the same holds for the “Title” column in non-library lists and also for the commands in the context menu of the item). In one of the comments of the original posting there was a request to me to demonstrate a solution or work-around for that which is what I am going to elaborate on in this posting. The solution is applicable for document libraries only and uses a custom computed field which renders as a link to the document which when clicked opens the correct version of the selected document. The link actually calls a small custom application page (that should be deployed to the “12/TEMPLATE/LAYOUTS” folder of the SharePoint installation) which with several lines of code using the SharePoint object model redirects either to the current document version or to one of its previous ones (with the _VTI_HISTORY URL pattern).

And the implementation itself, the schema XML of the computed field:

<Field ID="{ccccdddd-3710-4374-b433-61cb4a686c12}"

       ReadOnly="TRUE"

       Type="Computed"

       Name="VersionLinkFilename"

       DisplayName="Version link"

       DisplayNameSrcField="FileLeafRef"

       Filterable="FALSE"

       AuthoringInfo="(linked to version)"

       SourceID="http://schemas.microsoft.com/sharepoint/v3"

       StaticName="VersionLinkFilename" FromBaseType="TRUE">

  <FieldRefs>

    <FieldRef Name="FileLeafRef" />

    <FieldRef Name="FileRef" />

    <FieldRef Name="_UIVersion" />

  </FieldRefs>

  <DisplayPattern>

    <HTML><![CDATA[<a href=']]></HTML>

    <HttpVDir CurrentWeb="TRUE" ></HttpVDir><HTML><![CDATA[/_layouts/versionredir.aspx?FileRef=%2f]]></HTML><LookupColumn Name="FileRef" URLEncode="TRUE" /><HTML><![CDATA[&Version=]]></HTML><Column Name="_UIVersion" />

    <HTML><![CDATA['>]]></HTML>

    <LookupColumn Name="FileLeafRef" />

    <HTML><![CDATA[</a>]]></HTML>

  </DisplayPattern>

</Field>

The easiest way to add a computed filed to your document library is using code (or a custom tool), basically you can use the SPFieldCollection.AddFieldAsXml method (calling it on the Fields property of your SPList instance) providing the schema of the field in the first parameter.

And the code of the custom application page:

<%@ Page Language="C#" Debug="true" %>

<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Import Namespace="Microsoft.SharePoint" %>

<script runat="server">

    protected override void OnLoad(EventArgs e)

    {

        base.OnLoad(e);

 

        string fileRef = this.Request.QueryString["FileRef"];

        int version = int.Parse(this.Request.QueryString["Version"]);

 

        SPWeb web = SPContext.Current.Web;

        SPFile file = web.GetFile(fileRef);

        SPListItem item = file.Item;

 

        string redirUrl = null;

        if ((int)item["_UIVersion"] == version) redirUrl = web.Url.TrimEnd('/') + "/" + file.Url;

        else

        {

            foreach (SPListItemVersion v in item.Versions)

            {

                if ((int)v["_UIVersion"] == version)

                {

                    redirUrl = web.Url.TrimEnd('/') + "/_vti_history/" + version + "/" + file.Url;

                    break;

                }

            }

        }

        if (redirUrl != null)

        {

            Response.Redirect(redirUrl);

        }

    }

</script>

The page should be saved as “VersionRedir.aspx” in the LAYOUTS folder of your SharePoint hive.