Category Archives: SP 2010

Make your webpart titles localizable

In quite some projects, I had to localize some SharePoint 2010 pages or WebPart resources, which custom code can handle pretty well.

But when it comes to localize titles of WebPart, SharePoint does actually not provide anything but a single string for any language.

I then built an abstract class named LocalizableWebPart, which with a few helper classes helps you set the WebPart title by editing it and changing the current site display language.

Here is the content of this class so that you can have a look:

public abstract class LocalizableTitleWebPart : WebPart
{
    [WebPartStorage(Storage = Storage.Shared)]
    public string LocalizedTitle { get; set; }

    private LangageContainer langContainer = null;

    protected string GetLocalizedProperty(LangageContainer container, string content)
    {
        if (container != null)
        {
            return container.GetOrInsertLanguage().EncodedLanguageContent;
        }
        else
        {
            container = LocalizationHelper.GetLanguagesContent(content);
            return container.GetOrInsertLanguage().EncodedLanguageContent;
        }
    }

    protected string SetLocalizedProperty(LangageContainer container, string content, string value)
    {
        container = LocalizationHelper.GetLanguagesContent(content);
        var lang = container.GetOrInsertLanguage();

        if (this.Page != null && this.Page.IsPostBack)
        {
            container.UpdateLanguage(lang.LanguageCode, value);
            return LangageContainer.Serialize(container);
        }
        return content;
    }

    public override string Title
    {
        get
        {
            return GetLocalizedProperty(langContainer, this.LocalizedTitle);
        }
        set
        {
            this.LocalizedTitle = SetLocalizedProperty(langContainer, this.LocalizedTitle, value);
        }
    }
}

What you can see in the code above is:

  • one webpart property for all localized titles serialized as Xml (LocalizedTitle),
  • Methods to get and set localized content from and to the serialized string containing the title.

You can find the code of this class and all required classes as a GitHub repo here:

https://github.com/JaYmZ666/localizableTitleWebPart

Happy coding!

Join us for the Swiss Techdays 2013 in Lausanne!

Hi dear visitor,

This year Microsoft Techdays will be held at Conrès Beaulieu in Lausanne, on the 6th and 7th of November.

Yves Peneveyre and myself will be presenting a topic around SharePoint 2013 (in French :) )

As I will be available between sessions on the CTP booth, please feel free to come and visit us, and ask questions about our work and Windows Azure for example :)

See you there!

Building a custom Timer Job for SharePoint 2010 at farm scope: “Cannot change a recurring schedule to a one-time schedule (and vice versa) for a job definition”

I was trying to build a custom Timer Job hosted at Farm level, which looks like this from a Feature activation standpoint:

public override void FeatureActivated(SPFeatureReceiverProperties properties) 
{
    foreach (var s in SPFarm.Local.Services)
    {
        if (s.DisplayName.Contains("WSS_Administration"))
        {
            DeleteJob(s.JobDefinitions);
            CustomTimerJob job1 = new CustomTimerJob(s);

            SPMinuteSchedule schedule = new SPMinuteSchedule();
            schedule.BeginSecond = 0;
            schedule.EndSecond = 59;
            schedule.Interval = 1;
            s.JobDefinitions.Add(job1);
            job1.Schedule = schedule;
            job1.Update(true);
        }
    }
}

When trying to deploy, here is the error I was getting:

Cannot change a recurring schedule to a one-time schedule (and vice versa) for a job definition.”

Continue reading

SharePoint 2010 & Azure integration: Azure Blob Field !!!

For my last session in Techdays in Geneva this year, I proposed a session on the integration between SharePoint 2010 and Azure.

I am giving away today a custom column for SharePoint 2010 to upload download content to Azure Blob Storage !

What this thing does :

  • link the column instance in SP to a Blob Storage container,
  • as it is a custom Field, it integrates completely in SP and can leverage security and search capabilities,
  • it is dealing with upload / download with a Silverlight  client (using chunks for best performance),
  • this upload is based on Shared Access Signatures, to simplify security access for the Silverlight client.

Here is the link to the solution (all source code available) : http://sp2010azurefield.codeplex.com/

Happy download!

SharePoint 2010 and WCF Services

If you want to build some WCF service to be used in SharePoitn 2010 context, you came to right place :)

Here are steps to follow in order to create such services:

In Visual Studio 2010 create a blank SharePoint 2010 project, then add a “SharePoint mapped folder” to the SharePoint ISAPI folder:

Add these following assembly references to your project:

  • System.ServiceModel,
  • System.ServiceModel.Web,
  • Microsoft.SharePoint.Client.ServerRuntime.

Create a specific folder in ISAPI folder that will hold your WCF “.svc” service files (for example, mine is “MyWebservice”).

Create you class file,  that will hold your service interface and service implementation (I added 2 just to show creating a SOAP etand a REST service):

The important part in the file here are attributes used on service implementations:

  • BasicHttpBindingServiceMetadataExchangeEndpoint, which will allow service to expose a metadata exchange endpoint (a.k.a. MEX),
  • AspNetCompatibilityRequirements, required if service is to be used to access HTTPContext or SPContext objects.

Back in our ”ISAPI/MyWebservice” folder, let’s create 2 text files and then rename thier extensions as ”.svc”:

Edit those files and place the following code in it:

<%@ServiceHost
Language=”C#” Factory=”Microsoft.SharePoint.Client.Services.MultipleBaseAddressBasicHttpBindingServiceHostFactory,
Microsoft.SharePoint.Client.ServerRuntime, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”
Service=”SP2010WCFService.MyWebservice.MyWebserviceSOAP, $SharePoint.Project.AssemblyFullName$”%>
The red test here is the SharePoint factory that will be used to encapsulate your service to make it to run in the Sharepoint context ; 3 different factories are available:
  • MultipleBaseAddressBasicHttpBindingServiceHostFactory, fro a SOAP service,
  • MultipleBaseAddressWebServiceHostFactory, for a REST service,
  • MultipleBaseAddressDataServiceHostFactory, for a ADO.NET Data Service.

Once you choose your factory , you should have see that our host definition contains a specific tag, $SharePoint.Project.AssemblyFullName$ ? :)

This annotation coming out from SharePoint “Replaceable parameters” will replace corresponding values to SharePoint context values (here our compiled assembly details) ; for this to work when we’ll compile our stuff, edit the .Csproj file of your solution to add this element ”<TokenReplacementFileExtensions>svc</TokenReplacementFileExtensions>” in relevant “<PropertyGroup>” element so that Visual Studio will also replace parameters in SVC files.

Now just need to compile and have fun :)

PS: Here is the SP2010WCFServices solution shown in my example.

SharePoint 2010 InfoPath web forms deployment: it works

When developing InfoPath forms on an existing website, we could think how reimporting those customizations would work on a re-deployed package…

Answer is: YES !

Stil there are a few rules to follow in order to make it work fine:

  • Loopkup type field in a Content Type used in a list must point on a list in the same site as the list (in that case InfoPath is generating a secondary Data Connection automatically),
  • Manually created Secondary Data Connections must point also on a list in the same site as the list holding the edited form.

In hat case the export/import of the Template will work like a charm.

So here is a walkthrough for importing your saved site template:

First, once template is saved, open Visual Studio and select “Import SharePoint Solution Package”, and then the package you want to import :

Then, select important items (in our case the lists), unselect all with Crtl+A then Space then select revelant ones; Visual Studio will figure that out for you on next screen:

Once imported, we can see import done in Solution Explorer is using Modules to deploy XSN list templates.

Finally, we can clean up a bit default item names and add other stuff in it (Webpart…).

Happy coding :)