Archive for the Category ◊ SharePoint ◊

Author:
Thursday, February 14th, 2013

While using Content Query Web Part its always best practice to use custom Item Style so here we will see how to create new templates in itemxsl style and change the reference in .webpart to custom item xsl style

1. Navigate to Style library –> XSL style sheets –> Itemstyle.xsl

And download the local copy .

2. ItemStyle.xsl contains different templates e.g. Default, NoImage, TitleOnly, TitleWithbackground etc,So to create custom ItemStyle.xsl , save ItemStyle.xsl as CustomItemStyle.xsl . Delete all the templates and add the new template with different name say HRDocs

<xsl:stylesheet
version=”1.0″
exclude-result-prefixes=”x d xsl msxsl cmswrt”
xmlns:x=”http://www.w3.org/2001/XMLSchema”
xmlns:d=”http://schemas.microsoft.com/sharepoint/dsp”
xmlns:cmswrt=”http://schemas.microsoft.com/WebParts/v3/Publishing/runtime”
xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” xmlns:msxsl=”urn:schemas-microsoft-com:xslt”>
<xsl:output method=”html”/>
<xsl:param name=”ItemsHaveStreams”>
<xsl:value-of select=”‘False’” />
</xsl:param>
<xsl:param name=”FeedTitle” />
<xsl:param name=”WPPropertyBinding-Title” />
<xsl:variable name=”OnClickTargetAttribute” select=”string(‘javascript:this.target=&quot;_blank&quot;’)” />
<xsl:variable name=”ImageWidth” />
<xsl:variable name=”ImageHeight” />

<xsl:template name=”HRDocs” match=”Row[@Style=HRDocs]” mode=”itemstyle”>

<xsl:variable name=”SafeLinkUrl”>

<xsl:call-template name=”OuterTemplate.GetSafeLink”>

<xsl:with-param name=”UrlColumnName” select=”‘LinkUrl’”/>

</xsl:call-template>

</xsl:variable>

<xsl:variable name=”SafeImageUrl”>

<xsl:call-template name=”OuterTemplate.GetSafeStaticUrl”>

<xsl:with-param name=”UrlColumnName” select=”‘ImageUrl’”/>

</xsl:call-template>

</xsl:variable>

<xsl:variable name=”DisplayTitle”>

<xsl:call-template name=”OuterTemplate.GetTitle”>

<xsl:with-param name=”Title” select=”@Title”/>

<xsl:with-param name=”UrlColumnName” select=”‘LinkUrl’”/>

</xsl:call-template>

</xsl:variable>

<xsl:variable name=”LinkedInSafeImageUrl”>

<xsl:call-template name=”OuterTemplate.GetSafeLink”>

<xsl:with-param name=”UrlColumnName” select=”‘Linkedin’”/>

</xsl:call-template>

</xsl:variable>

<div>

<div>

<xsl:call-template name=”OuterTemplate.CallPresenceStatusIconTemplate”/> 

<div> 

<xsl:value-of select=”@documentname” />

</div>

<div>

<xsl:value-of select=”@docDescription” />

</div>

</div> </div> </xsl:template>

</xsl:stylesheet>

Things to take care while creating new template is to keep the same name for name and @style attributes( as below )

<xsl:template name=”HRDocs” match=”Row[@Style=HRDocs]“

and also add the <xsl:output method=”html”/> line  as added in above code to avoid the UI misalignement if any custom html is rendering null value.

3. Now upload this CustomItemStyle.xsl to same location ( Style Library –> XSL Style Sheets )

4. Now let’s make our CQWP to refer CustomItemStyle.xsl , open any page and add the CQWP and export the web part file which contains all the property tags , search for ItemXslLink

Which currently looks as

<property name=”ItemXslLink” type=”string”/>

Replace the above line with below

<property name=”ItemXslLink” type=”string”>/werkenbij/Style Library/XSL Style Sheets/CustomItemStyle.xsl</property>

Save the .webpart file as CustomCQWP.webpart and upload it to web part gallery [ Site Actions à Site Settings à Web parts (in Gallery)]

5. Add the web part on page , lets edit the web part to select the CustomItemStyle.xsl

Navigate to Presentation –>  Styles –>  Item Style and select CustomItemStyle.xsl from drop down.

 

That’s it guys your CQWP WITH CUSTOM Item style is ready to ship :)

Monday, December 17th, 2012

 

Dependency injection bindings through an IOC container can be achieved by different ways. For e.g. you can use an XML file based
configuration for registering types or use an imperative code block for registration. Both of these methods use an explicit way if registering and resolving types.
Another common and an easy approach is to use attributes to register and resolve types. In this post, we’ll extend the SharePoint service locator, to register types based on attributes to the container, which can be later used to resolve the mappings.
Creating the attribute:
public class SetAutoRegistration : Attribute, ISpAttribute
{   
    public Type InterfaceType { get; privateset; }
    public string Key { get; privateset; }
    public SetAutoRegistration(Type interfaceType) : this(interfaceType, string.Empty)
    {
    }
    public SetAutoRegistration(Type interfaceType, string key)
    {
        InterfaceType = interfaceType;
        Key = key;
    }
}

Creating a mappings object to define type mappings:
public class Mapping
{
    public Type FType { get; privateset; }
   
    publicType TType { get; privateset; }
   
    public string Key { get; privateset; }
    public Mapping(Type fType, Type tType, string key)
    {
        FType = fType;
        TType = tType;
        Key = key;
    }
}

Extending the ServiceLocator class to add type based mappings:
[SharePointPermission(SecurityAction.InheritanceDemand, ObjectModel = true)]
[SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
public void RegisterTypeMapping(Type fromType, Type toType)
{
    RegisterTypeMapping(fromType, toType, null);
}
[SharePointPermission(SecurityAction.InheritanceDemand, ObjectModel = true)]
[SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
public void RegisterTypeMapping(Type fromType, Type toType, string key)
{
    var typeMappings = GetConfigData();
    var newTypeMapping = newTypeMapping(fromType, toType, null) { InstantiationType = InstantiationType.NewInstanceForEachRequest };
    RemovePreviousMappingsForFromType(typeMappings, newTypeMapping);
    typeMappings.Add(newTypeMapping);
    SetTypeMappingsList(typeMappings);
}
[SharePointPermission(SecurityAction.InheritanceDemand, ObjectModel = true)]
[SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
public void RemoveTypeMappings(Type type)
{
    var typeMappings = GetConfigData();
    foreach (var mapping in typeMappings.ToArray())
    {
        if (mapping.FromType == type.AssemblyQualifiedName)
        {
            typeMappings.Remove(mapping);
        }
    }
    SetTypeMappingsList(typeMappings);
}

Using reflection to register types to service locator during feature activating:
var assembly = Assembly.Load(assemblyToProbe);
var typesToAdd = assembly.GetTypes().Where(x => x.GetCustomAttributes(typeof (SetAutoRegistration), false).Length > 0);
types.AddRange(typesToAdd);
var mappings = BuildMappingsFromTypes(types);
foreach (var mapping in mappings)
{
    if (mapping.Key == string.Empty) serviceLocatorConfig.RegisterTypeMapping(mapping.FType, mapping.TType);
    else  serviceLocatorConfig.RegisterTypeMapping(mapping.FType, mapping.TType, mapping.Key);
}

Attribute based registration in action:
[SetAutoRegistration(typeof(IMyRepository))]
public class MyRepository : BaseRepository<MyEntity>, IMyRepository
{
You can use the same approach to register types to activating service locator for the test cases.
Sunday, December 16th, 2012

 

The SharePoint Service Locator is a reusable component from the MS Patterns & Practices that provides a simple implementation of the service locator pattern. You can use it in your own SharePoint applications to decouple the consumers of an interface from the implementations of that interface. Instead of creating an object by invoking the constructor of a class, you request an object with a specified interface from the service locator. The service implementation can now be replaced or updated without altering the consuming class implementation.
The usage of service locator makes it easy to dependencies that are scattered throughout various projects and solutions. These
dependencies often make it challenging to maintain code over time—if you modify one class, you must recompile every project that references that class. This also makes unit testing code in isolation much more complicated. In short, decoupling your code from specific types makes your code more modular, easier to manage, and easier to test. You can use the RegsiterTypeMapping
method of the ServiceLocatorConfig instance to register the mappings to the service locator.
var serviceLocator =SharePointServiceLocator.GetCurrent();
var mappingsConfig = serviceLocator.GetInstance<IServiceLocatorConfig>();
mappingsConfig.RegisterTypeMapping<IMyService, MyService>();
You can retrieve the interface implementation later in your code using the GetInstance method of the ServiceLocatorConfig instance.
serviceLocator.GetInstance<IMyService>();
Normally you register the instances during the feature activating event and you need to remove the registered mappings from the service locator during feature deactivating event using the remove mappings method.
var serviceLocator =SharePointServiceLocator.GetCurrent();
var mappingsConfig = serviceLocator.GetInstance<IServiceLocatorConfig>();
mappingsConfig.RemoveTypeMapping<IMyService>(null);

Unit testing challenge – Replacing the ServiceLocator in the test context
The service locator from the P&P uses the sharepoint object model to keep store of the mappings and other properties. To
successfully unit test the code that uses the service locator, you need to either use a wrapper object for the service locator or use the ActivatingServiceLocator object instead of the ServiceLocator in the test code.
To replace the service locator instance, you need to replace the actual service locator with the ActivatingServiceLocator
instance as given below.
var activatingServiceLocator =newActivatingServiceLocator();
SharePointServiceLocator.ReplaceCurrentServiceLocator(activatingServiceLocator);
conststring type1 =“Type1″;
conststring type2 =“Type2″;
activatingServiceLocator.RegisterTypeMapping<INotRegisteredType, NotRegisteredType1>(type1);
activatingServiceLocator.RegisterTypeMapping<INotRegisteredType, NotRegisteredType1>(type2);
var notRegisteredType =SpServiceLocator.GetInstance<INotRegisteredType>(type1);
Assert.IsInstanceOfType(notRegisteredType, typeof(NotRegisteredType1));
SharePointServiceLocator.Reset();
Sunday, December 09th, 2012

 

With the availability of Visual Studio 2012 SharePoint Emulators the isolation of the SharePoint API is much easier compared to the
old way of isolating using the Moles or Fakes framework.  In order to incorporate emulators into existing tests you should wrap the relevant code in a SharePointEmulationScope, which accepts EmulationMode enum as a parameter. It is enabled by default which in turn performs run time interception of all Microsoft.SharePoint.dll calls.
You can use the new emulator in your unit test project by using nuget packaging manager and convert your old test case to the new
emulation scope as given below 
 [TestMethod]
publicvoid BuildFromSPListItemShouldCreateANewObjectWithPopulatedValues()
{
    using(ShimsContext.Create())
    {
        var dataMapper = newMyObjectDataMapper();
        conststring customDescription = “Custom
Description”
;
        var spListItem = newShimSPListItem
        {
            ItemGetString = (fieldName) =>
            {
                if (fieldName == BaseInternalFields.Id) return1;
                if (fieldName == BaseInternalFields.Title)
                    return“Some Title”;
                return fieldName == RulesInternalFields.CustomDesc
                    ? customDescription
                            : null;
            },
            FieldsGet = () => newShimSPFieldCollection
            {
                ContainsFieldString =
                    (internalName) =>
                    internalName.Equals(BaseInternalFields.Id) ||
                    internalName.Equals(BaseInternalFields.Title)
||
                    internalName.Equals(RulesInternalFields.CustomDesc)
            }
        };
        var myObject = dataMapper.FromSPListItem(spListItem);
        Assert.IsTrue(myObject.CustomDesc == customDescription);
    }
}


[TestMethod]
publicvoid BuildFromSPListItemShouldCreateANewObjectWithPopulatedValues
()
{
    using (var emulation = newSharePointEmulationScope(EmulationMode.Enabled))
    {
        var dataMapper = newMyObjectDataMapper();
        conststring customDescription = “Custom
Description”
;
        using(var emulatedSite = newSPSite(“http://localhost:8081/MySite”))
        {
            var listId = emulatedSite.RootWeb.Lists.Add(“MyList”, “Custom list”, SPListTemplateType.GenericList);
            var list = emulatedSite.RootWeb.Lists[listId];
            var listitem = list.Items.Add();
            listitem["Title"] = “mytitle”;
            listitem["CustomDesc"] = “Custom Description”;
            var myObject = dataMapper.FromSPListItem(listitem);
            Assert.IsTrue(myObject.CustomDesc == customDescription);
        }
    }
}

Because the emulator relies on the Fakes framework, you can easily reuse your existing code to use the emulators without much pain.
Author:
Tuesday, June 01st, 2010

I had been a SharePoint developer for some days before shifting focus on to Silverlight and WCF.

But recently, in my new organization I took up the initiative for introducing SharePoint, thereby donning all the hats, but beginning with Administrator.

One of the primary questions was “What are the licensing models for SharePoint 2010?

To my bewilderment, there weren’t many straight answers to this question. Below I am just trying to make a mere mortal attempt to unravel mysteries surrounding SharePoint 2010 licensing.

To begin with there are two modes of hosting any application on SharePoint

  • On-Premise – We are hosting the SharePoint server within the organization premises.
  • SharePoint Online – Using cloud technologies provided by Microsoft to host the application.

On-Premise Hosting
To start off, SharePoint 2010 maintains the similar model to that of 2007 i.e. It has mainly 3 variations 

Well before actually getting to licensing models, MS uses the term Client Access Licenses (CAL) quite freely around these topics. More information on CAL can be found @ http://www.microsoft.com/licensing/about-licensing/client-access-license.aspx.

Now to the actual licensing modes, each version i.e. the “Standard” and “Enterprise” versions comes with 2 flavors of licensing model viz. “Intranet” and “Internet”

As the name suggests each licensing model is targeted to the specific usage of the platform.
 


As illustrated above, Enterprise editions can be upgraded with FAST (http://www.microsoft.com/enterprisesearch/en/us/fast.aspx) search licenses.

All said, the single eternal question that remains are :

“How much damn do I need to spend for all these !!!!!!!”

Well the simple answer for that would be – “It depends” :)

To elaborate on that, it depends

  • Whether you are using SharePoint for intranet or Internet or for both?
  • Do you require Enterprise/Standard/Foundation edition?
  • What kind of deployment?

The next question(atleast I had this) was isn’t SharePoint foundation completely free? So can I just use it?

SharePoint Foundation 2010 is itself a free product that requires no CALs. The caveat is, SharePoint foundation itself needs the following -

  • One or more servers which need to be correctly paid for and licensed

    = (Cost for the server) + (Server CALs for the users accessing the server ) + (Internet Connection License if server is connected to Internet). 

  • A database system to store its databases.
    • If “Basic” installation is used, then there will be single server installation which uses SQL 2008 express. SQL 2008 Express is completely free of any CAL’s.
    • If “Advanced” installation is used, then SharePoint 2010 foundation uses either SQL server 2005/2008, which in-turn needs to be licensed.

** In both the Server and the SQL Server cases, there are (“enterprise”) licensing options available that mean that no CALs are required .

Well that is pretty much it for On-Premise hosting of SharePoint, shall continue with cloud based options in the coming post.