For a SharePoint 2010 demo i’m building a web part that should be accessible by Mobile devices. In SharePint 2010 all mobile devices are redirected by default to the mobile SharePoint rendering engine.
The mobile rendering engine of SharePoint provides mobile users access to the sites with a completely stripped interface. Its main function is to list all libraries and to provide basic access to them. For wiki and blog sites the rendering engine also provides some content rendering.
To keep the amount of data as low as possible, almost all web parts are completely stripped from the mobile site. Only when a web part developer provided a special mobile version of the web part, the web part will be accessible in the interface and to keep the mobile screen as empty as possible, the user must first expand the web part via its title to see it.
Documentation about SharePoint Mobile development is provided at MSDN. At first it all looks quite complex, but building a mobile version of a web part is actually not that hard. Below I will explain the required steps with a ‘Hello world’ example. For this sample I will not rename the default object names provided by the Visual Studio templates.
Create a sample site
In this sample we begin with a fresh empty site. Create a new “Blank Site” and call it “Mobile Demo”.
Create a Visual Studio 2010 Visual Web Part Project
Visual studio 2010 includes a full set of SharePoint templates. These templates automatically publish and debug the project to your SharePoint environment.
Start a new “Visual Web Part Project” and name it “MobileWebPartDemo”.
Add a literal with the “hello world” message
We will now create the normal SharePoint web part. We only show a sample message by adding a literal to the web part controls.
Open the class file “VisualWebPart1UserControl.ascx.cs” and add the following code to the load event:
litHello.Text = “Hello World. This is the normal SharePoint web part.”;
Controls.Add(litHello);
Build and add the web part to the page
Press F5 to compile and build the web part. Internet explorer will start. Browse to the “Mobile demo” sample site and open the default page in edit mode. Add a new web part. Inside the “Custom” category you will find our demo web part. Add “VisualWebPart1” to the page.
Preview the site
Exit the edit mode of the page. You will now see your site and the web part as normal users will see it.
Preview the mobile site
To view the web site in mobile view, type a “/m” after the normal site url. For example: “http://localhost/mobiledemo/m/”.
At this moment the website is rendered in the mobile view. But hey! We don’t see the web part we just published. This is because the engine strips all normal web parts to keep the data transfer as low as possible. Only mobile optimized web parts will be shown.
Create the mobile (adapter) version of our web part
To add our web part to the mobile version of the site we must provide SharePoint with a mobile optimized version of our web part. We do this by adding a new class. Microsoft calls this class an adapter.
Add a new class and name it “VisualWebPart1MobileAdapter.cs”. Note that we added the name of our web part in front and added the text “MobileAdapter” at the end.
Microsoft recommends to use the namespace “SharePoint. WebPartPages.MobileAdapters” so modify the namespace to use “MobileWebPartDemo.SharePoint.WebPartPages .MobileAdapters”.
Our class should inherit the “WebPartMobileAdapter” class.
The adapter does not use the “OnLoad” event. Instead it uses the “CreateControlsForDetailView” to render. Override this class and add the following code:
litHello.Text = “Hello World. This is the Mobile version of the SharePoint web part.”;
Controls.Add(litHello);
Add the adapter to the safe controls
SharePoint should know that the adapter web part is a safe control. We did not have to add the normal web part as safe control, because the project template already fixed this for use. However, we still have to add the mobile version.
Open the web.config file of your SharePoint IIS site (example: C:inetpubwwwrootwssVirtualDirectories80) and add the line below into the safecontrols section. Replace the PublicKeyToken with your own key!
Configure the compat.browser file to use our mobile web part
SharePoint must be told that there is a mobile optimized version available for our web part. Basically we are going to tell SharePoint that if there is web part “Normal” on the page, it should load version “Mobile”. Without this configuration SharePoint would just strip our web part like before.
Inside your SharePoint IIS directory you will see a directory “App_Browsers” and inside that directory is a file called “compat.browser”. Load this file in the editor.
At the top of this file you will find a “controlAdapters” section. Add the line below to this section:
Again, replace the PublicKeyToken with your own key! Safe the configuration and exit the file.
Rebuild the project and reload the mobile demo site
We finished the required steps to provide SharePoint with a web part that is optimized for the mobile page. Press F5 to build and deploy our project and re-open the mobile view of our demo site. And wow it’s almost magic! The title of our web part has suddenly appeared.
Expand the web part
To keep the mobile screen as empty as possible, not all web parts are automatically visible. Just click the title of the web part and it will be rendered.
And that’s it. This is all the information required to start developing web parts for the SharePoint Mobile pages. Enjoy!
November 12, 2010 at 1:25 am
Nice blog post. I’ve following your instructions word for word and for some reason the mobile web part simply doesn’t show up. I also followed the MS example and still nothing. Have you ever seen this? There are no errors either.
February 9, 2011 at 6:32 pm
Great! article. it works for me.
March 4, 2011 at 10:38 am
Nice blog.. But i come across a configurational error which i couldnt solved…
Here is error..
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.
Parser Error Message: Could not load file or assembly ‘MobileWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=41167d1e0c5a217f’ or one of its dependencies. The system cannot find the file specified.
Source Error:
Line 3:
Line 4:
Line 5:
Line 7:
Can u pls help to over come this?
March 4, 2011 at 10:41 am
Sorry the error is following line.
Thanks in advance
April 6, 2011 at 10:24 am
A very helpful blog indeed Edwin. Initially got a couple of errors but could manage to establish the connection and create the mobile version of my custom webpart. But in another try i have created another custoem webpart which has a standard login page consisting of 2textboxes,2 buttons.I have not been able to view this web part in the mobile view of the page. a certain code should go into the CreateDetailView() method of the adpater class and im not able to find the needed code. Could you kindly help in provinding insights with th same or any kind of materials where i can refer and solve this.
Thanks in advance.
June 15, 2011 at 3:51 pm
errors …
June 15, 2011 at 3:53 pm
it is confusing , not clear…..but good try
June 15, 2011 at 4:01 pm
throws error here : <adapter controlType=”MobileWebPartDemo.VisualWebPart1.VisualWebPart1, MobileWebPartDemo, Version=1.0.0.0, Culture=neutral,
July 11, 2011 at 7:43 am
Here is a nice article that I thought fit nicely with this from EPC Group’s Errin O’Connor regarding mobility in SharePoint 2010: http://www.aiim.org/community/blogs/expert/SharePoint-2010-Mobility-Browser-Compatibility-Compliance-and-its-Continued-Dominance-in-the-Marketplace
July 18, 2011 at 5:50 pm
When you say, “Replace the PublicKeyToken with your own key,” to which PKT are you referring? I’ve tried several that I have found, but none are working. Maybe I’m not looking in the right place.
July 18, 2011 at 10:00 pm
Alright – never mind… forgot to add that pesky “public” modifier to my new mobile adapter class…
August 11, 2011 at 1:26 pm
This is a awesome post..!!! Thank you 🙂
August 26, 2011 at 9:06 pm
How do you create a solution package (wsp) cannot deploy to App_Browsers folder?
September 28, 2011 at 2:18 pm
I am getting this error
Server could not create MobileWebPartDemo.SharePoint.WebPartPages.MobileAdapters.VisualWebPart1MobileAdapter.
please help
January 26, 2012 at 8:51 pm
Nice Blog on mobile web part….Thanks for that…
finally i could create it
but it is throwing me a javascript error(object expected) after huge effort
any ideas?????
March 14, 2012 at 4:59 am
Can these webparts be placed in custom mobile pages ? if yes can you tell me how. Thanks in advance
August 27, 2012 at 8:31 am
its not working on publishing site.
when i click on expand icon it does not expand the webpart