onsdag 19 maj 2010

Tech Days 2010, blog posts and start with Bing Maps

Speaking Swedish or understand Swedish? If you are interested in BIng Maps, SharePoint, DeepEarth, Microsoft and GIS you can watch my presentation at Tech Days 2010 in Örebro, Sweden the 24th of Mars this year.

Lately Johannes Kebeck, a Bing Maps TSP blogged about my favorite project SMIL. That blog post is one post in a blog series about integrating maps into SharePoint. His latest post is about integration themetic maps from SQL Server 2008 R2.

If you are intested in starting developing with Bing Maps you should really go to Bing Maps portal. Johannes Kebeck, Chris Pendleton and BingMapsDev are people you can follow on twitter to get the latest news. Another good start is to go the developer forum. When should also have a look at Deep Earth Toolkit for Bing Maps.

onsdag 12 maj 2010

Creating a Silverlight Web Part for SharePoint 2007

In this blog post I describe an approach to add a Silverlight Web Part in SharePoint 2007. Adding Silverlight Web Parts to SharePoint can really make magic to SharePoint, i.e. maps (my personal favorite topic), Web Parts with drag and drop support from the file system etc. I always create a Silverlight project and separate and load it to a web part. I describe my favorite approach:

  1. Create a Web Part project, rename the Web Part and it’s files to something appropriate. I use Visual Studio 2008 extensions for Windows SharePoint Services 3.0 (version 1.2) in this example.
  2. Create a folder name it Templates.
  3. Create a folder name it ControlTemplates
  4. Create a folder name it YourWebPart-name.
  5. Choice File –> Add –> New Project. Choose web and give it a name.
  6. Get rid of the Default.aspx file in the web project.
  7. Add a User Control to the web project, get rid of the code behind and add your the reference to the web part assembly to the inherits of the file, example: <%@ Control Language="C#" AutoEventWireup="true"  Inherits="MyNameSpace.ClassName, AssemblyName, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=3283a693cab37f84" %> See understanding SharePoint for more info. The nice part now is that you can start designing the user control as with any .aspx-file, well almost. Add your .xap file and Silverlight.js-file to this project. 
  8. Add javascript and code for loading your silverlight control, i.e
    <div id="silverlightMultiFileUploadControlHost" >
    <object id="MultiFileUploader" data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="450" height="280">
    <param name="source" value="/_controltemplates/mPost.MultiFileUpload/mpost.SilverlightMultiFileUpload.xap" />
    <param name="onerror" value="onSilverlightError" />
    <param name="initParams" value="MaxFileSizeKB=,MaxUploads=2,FileFilter=,ChunkSize=4194304,CustomParam=[site=<%=Site%>;docLib=<%=DocumentLibrary%>;httpService=<%=HttpService%>],DefaultColor=White" />
    <param name="background" value="white" />
    <param name="onload" value="pluginLoaded" />
    <param name="minRuntimeVersion" value="4.0.50401.0" />
    <param name="autoUpgrade" value="true" />
    <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.5040


    1.0" style="text-decoration:none">
    <img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight" style="border-style:none"/>
    </a>
    </object>
    <iframe style='visibility: hidden; height: 0; width: 0; border: 0px'></iframe>
    </div>




  9. Add properties to your .cs file. I always add a SPWeb property to access the web from the user control and make sure that the .designer.cs file has the same class name.


  10. Go back to your web part project and add the following methods:

         protected override void OnLoad(EventArgs e)

            { SilverlightWebPart


                ScriptManager scriptManager = ScriptManager.GetCurrent(Page);


                if (scriptManager == null) 
                {  scriptManager = new ScriptManager();


                    Controls.Add(scriptManager); 
                } 
            }
     
            protected override void OnPreRender(EventArgs e)

            {


                base.OnPreRender(e);


                ClientScriptManager cs = Page.ClientScript; 
                if (!cs.IsClientScriptIncludeRegistered("sl_javascript"))


                    cs.RegisterClientScriptInclude(this.GetType(), "sl_ja vascript", "~/_controltemplates/mPost.MultiFileUpload/Silverlight.js");


                if (!cs.IsClientScriptIncludeRegistered("spsl_javascript"))


                    cs.RegisterClientScriptInclude(this.GetType(), "spsl_javascript", "~/_controltemplates/mPost.MultiFileUpload/SpSilverlight.js");


            }


  11. Of course you need to change the script source to your path.


  12. Add reference System.Web.Extensions


  13. Choóse add existing item in your web part project. Add your user control as a link (we want to compile it with the web part assembly), also add a Silverlight.js and your .xap file the same way to this folder.


  14. You’re done, all you have to do now is to load the user control to the web part in the CreateChildControls method like:
    mPost.MultiFileUploadWebPart.MultiFileUploadUC uc = (mPost.MultiFileUploadWebPart.MultiFileUploadUC)
    Page.Page.LoadControl("~/_controlTemplates/mPost.MultiFileUpload/MultiFileUploadUC.ascx");
    Controls.Add(uc);




In 14 steps I can write shiny cool Silverlight web parts for SharePoint 2007. There are some steps, especially with the 5 part assembly that is a little tricky. Of course my favorite Silverlight web part is the one in SMIL and Silverlight Multi File Upload that has inspired this blog post. See http://slfileupload.codeplex.com/SourceControl/changeset/view/47874#1002455 for a working code example.




tisdag 4 maj 2010

SharePoint MultifileUpload – HTTP Handlers and “the security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again”

In a current project I added a custom HTTP handler for a silverlight multifile upload web part for SharePoint. First, it’s obvious I need a client and a server pattern for make this happen. The client is just a Silverlight Web Part, nothing special nothing fancy. The server is a HTTP-handler, i.e an .ashx-file.

Integrate ASHX file into SharePoint

In order to integrate http-handler my favorite approach is to use WSPBuilder. Add a new WSPBuilder project, close it and open your .csproj file in notepad, or your favorite text editor, insert the following line right after <PropertyGroup>:

<ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>

Compile the project and find your public key token, either by writing
sn –T dll-file in the .NET console window or by just adding to dll to the GAC.

Now, all you need to do is create a directory structure in the hive 12 structure. I.e 12/Layouts, it’s common practice to add a directory after Layouts, in my case MultiFileUploadStructureMultifileUpload. The next step is just  to add Generic Handler to your solution. When you’ve done that you need to reference to your assembly in the GAC on the following pattern: AssemblyName, Version, Culture, PublicKeyToken. Example:

<%@ Assembly Name="MultiFileUpload, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a8aa1afe43cd31d3" %>

My complete markup is as follows:

<%@ Assembly Name="MultiFileUpload, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a8aa1afe43cd31d3" %>
<%@ WebHandler Language="C#" CodeBehind="MultiFileUpload.ashx.cs" Class="MultiFileUpload.MultiFileUpload" %>

By now you’re all set, you should be able to open IIS-manager and browse to the .ashx-file and open it in a web browser. Make sure the .ashx-file is at least in the C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS folder.

In my case I uploaded files to a document library with a Silverlight web part, in order to make that to work I needed to add:

site.AllowUnsafeUpdate = true.

This is strange because I make the call from a web browser inside SharePoint, perhaps it has something to do that it is actually a Silverlight web part?

The web part and the server code is given back to the project. http://slfileupload.codeplex.com/.

More info:MSDN, myrocode.com, Mike Poulson.

Technorati Tags: ,