Tuesday, 3 July 2012

Create a custom SitePage

A SitePage is a static page that you create and sits in the pages library of your web; You may wonder why use Visual Studio for this if you could just use designer? valid question, and the long answer is you have more customization ability, you can add a codebehind, in my case I'm much more comfortable with visual studio. In short, I hate designer and I hate no code solutions I'm a full code solution kind of guy; the main advantage of a full code solution is dumb people can't ruin them, and if you think your organization doesn't have dumb people, then it breaks my heart to inform you that you are most likely one of them.

To get started create a module inside a project, if you don't know what I'm talking about I recommend you open ms paint and doodle for the rest of the day, then pursue employment as a sandwich artist.

In your solution Explorer set focus to where you want your SitePage (I Recommend a Folder called SitePages) and hit (ctrl+shift+a) this will bring up the add new item dialogue.



Make sure you have SharePoint 2010 selected in the left hand column, select Module in the middle pane, give your module a descriptive name and finally click the Add Button.

Once you add the Module to your solution it will have two visible files and one hidden one inside of it, don't worry too much about the hidden one. The two we're concerned with are the elements file which basically describes how and where to deploy your site page and a sample.txt file which will end up being your what; rename the sample.txt file to your site page, I'm going with mobileHomePage.aspx.


 What you should end up with is:


With your SitePage created, go ahead and open her up to see what's inside.

Not exactly what you where hoping for, but he no worries I'm here to hold your hand and tell you it's going to be OK, paste the following code over top of the place holder.

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, 
                   PublicKeyToken=71e9bce111e9429c" %> 
 
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" 
             Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, 
                       PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" 
             Assembly="System.Web.Extensions, Version=3.5.0.0, 
                       Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
 
<%@ Page language="C#" 
         masterpagefile="~masterurl/custom.master" 
         Inherits="Microsoft.SharePoint.Publishing.PublishingLayoutPage,
                   Microsoft.SharePoint.Publishing,Version=14.0.0.0,
                   Culture=neutral,
                   PublicKeyToken=71e9bce111e9429c" 
   meta:progid="SharePoint.WebPartPage.Document" %>
 
<asp:Content ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
    <SharePoint:FieldValue id="PageTitle" FieldName="Title" runat="server" />
</asp:Content>
 
<asp:Content id="Content1" runat="server" contentplaceholderid="PlaceHolderMain" />

With that complete all that's left is the elements file, what we just did think of as the what where as the elements file is more so the where and how, not really but it's a sufficient enough explanation. Go ahead and open the elements file what you should see is:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="MobileHomePage">
    <File Path="MobileHomePage\Sample.txt" Url="MobileHomePage/Sample.txt" />
  </Module>
</Elements>
change it to:
<?xml version="1.0" encoding="utf-8"?>
 
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="MobileHomePage" Url="$Resources:cmscore,List_Pages_UrlName;" Path="">
    <File Path="MobileHomePage\MobileHomePage.aspx" Url="MobileHomePage.aspx"  
          Type="GhostableInLibrary" IgnoreIfAlreadyExists="True">
      <Property Name="Title" Value="Home" /> 
      <Property Name="ContentType" Value="$Resources:cmscore,contenttype_pagelayout_name;" />
      <Property Name="PublishingPreviewImage"  
                Value="~SiteCollection/_catalogs/masterpage/$Resources:core,Culture;/Preview Images/DefaultPageLayout.png, ~SiteCollection/_catalogs/masterpage/$Resources:core,Culture;/Preview Images/DefaultPageLayout.png" />
     </File>
  </Module>
</Elements>

and you are done, deploy your solution check your pages library of your root web and it should be there. if it's not well best of luck.

So this is great right? But may recall that one of the benefits I listed was a code behind, so before you call me a liar, lets go ahead and do that. First step would be to create an actual file for the code behind so lets do that, select your module and add a new item again. Not to insult your intelligence, well maybe just a little refer to the image below.
Again this will bring up the add new item dialogue.


This time select Code under Visual C# in the first pane, select Class in the second pane, and give it the exact same name as your SitePage, just end it with a .cs; so my SitePage was MobileHomePage.aspx so my code behind page is MobileHomePage.aspx.cs.

Make sure you have a reference in your project to Microsoft.SharePoint.Publishing, not sure how? Two words: Sandwich artist...

once you click add reference, you'll get the add reference to ..... dialogue, in the left pane under Assemblies select Extensions, then in the middle pane locate the reference you want to make, in this case it's Microsoft.SharePoint.Publishing, once you select it click the Add Button and you should see the following if you did it correctly.

 
With that complete when you open up your CodeBehind, this is what you'll see:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace CCW.SharePoint.Mobile.SitePages.MobileHomePage
{
    class MobileHomePage
    {
 
    }
}
change it to
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
using Microsoft.SharePoint.Publishing;
using System.Web.UI.WebControls;
 
namespace CCW.SharePoint.Mobile.SitePages.MobileHomePage
{
    class MobileHomePage : PublishingLayoutPage
    {
 
    }
}

With that complete we have to go back to the .aspx page and make the connection to the code behind.
Change the page declaration from:
<%@ Page language="C#" 
         masterpagefile="~masterurl/custom.master" 
         Inherits="Microsoft.SharePoint.Publishing.PublishingLayoutPage,
                   Microsoft.SharePoint.Publishing,Version=14.0.0.0,
                   Culture=neutral,
                   PublicKeyToken=71e9bce111e9429c" 
  meta:progid="SharePoint.WebPartPage.Document" %>
to:
<%@ Page language="C#" 
         masterpagefile="~masterurl/custom.master" 
         CodeBehind="MobileHomePage.aspx.cs"
         Inherits="CCW.SharePoint.Mobile.SitePages.MobileHomePage.MobileHomePage" %>
 note that the namespace for the Inherits property is going to be different for your page.

Wednesday, 27 June 2012

Rapid CSS & JS Updates

When tinkering with JavaScript or Styling it is imperative to quickly be able to see your changes, where one obvious option would be to find your css or js in the hive folder and modify it directly, you could potentially loose a lot of work with a small oversight. What I like to do is make a batch file to overwrite my JavaScript and CSS in the hive with the files from my development folders, this way all i have to do is save and run my batch file to update CSS and JS.

to make a batch file just right click on your desktop and create a new text file.
find the path where your development CSS or JS file resides

C:\Users\Administrator\Documents\Visual Studio 2010\Projects\Sharepoint Console\Mobile.MasterPage\Layouts\Mobile.MasterPage

then find where it deploys to in the hive 14 folder

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\Mobile.MasterPage\

using the command xcopy with a switch /y you should end up with

xcopy "C:\Users\Administrator\Documents\Visual Studio 2010\Projects\Sharepoint Console\Mobile.MasterPage\Layouts\Mobile.MasterPage" "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\Mobile.MasterPage\" /y

save your text file with the extension .bat

now when you run your batch file it should copy everything from one directory to the other overwriting the changes, and hence saving you the bother of deploying your solution to see simple client side scripting and branding changes.

make sure to hold ctrl + f + f5 in firefox when you refresh your page to grab the latest CSS and JS files

Thursday, 14 June 2012

Add Connection String to webconfig.xml

You know you should never modify the webconfig.xml file manually, so lets talk about how to add a connection string through XML. Now keep in mind that this technique can be used to add or remove anything to your webconfig.

First thing you need to do is map the config folder to your project:
  • right click on your project in the solution explore
  • scroll over Add
  • Click on SharePoint Mapped Folder
The "Add SharePoint Mapped Folder" dialogue should pop up, select the config folder.
Once you click OK, the mapped folder should appear in your solution explorer

 
 With the CONFIG mapped folder press ctrl+shift+a to add a new item.
  • in the left hand pane select Data
  • in the middle pane select XML File
  • at the bottom make sure to name your file webconfig.*.xml; it is imperative that you follow this naming format. (webconfig.addConnections.xml)
Once you click the add button you will get an empty xml file to add webconfig modification to
 
add the following to your xml file

<actions>
  <add id ="{BE573280-EDD2-4E9D-9C53-2B943A118F63}" path = "configuration" >
    <connectionStrings />
  </add>
  
  <add id ="{6B92AB06-90B9-4464-BEF5-C0F5B14FABA6}" path = "configuration/connectionStrings" >
    <add name="ConnectionStringName" 
         connectionString="Data Source=myServer;Initial Catalog=INTERNET;User ID=userID;password=Password;" />
  </add>
</actions> 
 
Make sure to use unique id's for your actions otherwise you'll get multiple entries everytime you update your webconfig.
  1. With your XML ready to go, deploy the project.
  2. Once you've deployed your project successfully open up windows powershell (not the ISE) and run the command "stsadm -o copyappbincontent"
 

Presto, all should be well

But if it's not continue to read.

Should you get an error that says "expression must evaluate to a node-set"
that means that you didn't build the path correctly, notice:
first i add the connectionStrings element
  <add id ="{BE573280-EDD2-4E9D-9C53-2B943A118F63}" path = "configuration" >
    <connectionStrings />
  </add>
Second I add the connection string to the the connectionStrings element
  <add id ="{6B92AB06-90B9-4464-BEF5-C0F5B14FABA6}" path = "configuration/connectionStrings" >
    <add name="ConnectionStringName" 
         connectionString="Data Source=myServer;Initial Catalog=INTERNET;User ID=userID;password=Password;" />
  </add>
 
 Finnally if you see this
 
Somewhere there is a duplication, it doesn't necessarily have to be what you did, it could have been someone else breaking the number one law of webconfigs: Thou Shalt Not Manually Edit.