3

How to hide some of the SharePoint FormMenuItems buttons

To hide some of the FormMenuItem Buttons on a New, Display or Edit form in SharePoint is very easy.








All you have to do is to add a bit of Javascript to the form. You can quickly do this via the SharePoint designer.

Open the form on which you want to hide the menu item button in SharePoint Designer. Once opened switch to code view.



















Once you have the code view open, find the PlaceHolderBodyAreaClass section and insert the following code:

<script type="text/javascript">
  alert('Hello');
</script>










Save the form and hit F12 to view in browser.
You should now see an alert message popup when you access the form.
If you go the following message then it means that your Javascript is executing without any problems and that you can now proceed to add the relevant code to hide the desired buttons.
 
 
 
 
 
 
 
 
 
 
 
So, in SharePoint Designer, at the same location where you added the Javascript code, replace the following code:
<script type="text/javascript">

alert('Hello');
</script>
with:
<script type="text/javascript">

  hideFormMenuItems("Delete Item");


  function hideFormMenuItems()
  {
   var titleToHide="";
   var anchorTag;
   var allAnchorTags = document.getElementsByTagName('a');
   for(var i = 0; i < hideFormMenuItems.arguments.length; i++ )
   {
    titleToHide = hideFormMenuItems.arguments[i];
    if(titleToHide!='Alert Me')
    {
     for (var j = 0; j < allAnchorTags.length; j++)
     {
      anchorTag= allAnchorTags[j];
      if (anchorTag.title.indexOf(titleToHide)!=-1)
      {
anchorTag.parentNode.parentNode.parentNode.parentNode.parentNode.style.display="none";
anchorTag.parentNode.parentNode.parentNode.parentNode.parentNode.nextSibling.style.display="none";
       break;
      }
     }
    }
    else
    {
     for (var k=0; k < allAnchorTags.length;k++)
     {
      anchorTag= allAnchorTags[k];
      if (anchorTag.id.indexOf("SubscribeButton")!=-1)
      {
anchorTag.parentNode.parentNode.parentNode.parentNode.parentNode.style.display="none";
       break;
      }
     }
    }
   }
  var allSpanTags = document.getElementsByTagName("span");
  var spanTag;
  var toolbarRow;
  var lastCell;
  for(var m=0; m < allSpanTags.length;m++)
  {
   spanTag = allSpanTags[m];
   if(spanTag.id=='part1')
   {
    toolbarRow = spanTag.childNodes[2].firstChild.firstChild;
    lastCell = toolbarRow.lastChild.previousSibling
    while(lastCell.style.display=='none')
    {
     lastCell = lastCell.previousSibling;
    }
    if(lastCell.innerText == '')
    {
     lastCell.style.display='none';
    }
    break;
   }
  }
 }
</script>


So, when done, save your form and run.... you will see the "Delete Item" button is hidden.
You can now add more javascript to only hide the button based on certain conditions.



Enjoy!!






8

SharePoint Page Code Behind - Reference to Assembly and Namespace

Yesterday someone asked me how to resolve the "assembly x could not be loaded" when using custom code behind to aspx pages in SharePoint. The trick was that the class namespace was different than the project name, the assembly was placed in the GAC, and I think the class was not declared as public partial.

So, this tutorial will quickly show you the steps to add a code behind to a custom SharePoint page and then how to modify the web.config and the page code to use the custom code. We will then change the namespace so that the project / assembly name is different to the namespace name and we will see the impact that has on SharePoint and how we should fix it.

Create a new sandbox SharePoint site and open the site in SharePoint Designer. (WSS 3 or MOSS 2007)
Start with a vanilla (unmodified) site to ensure that other problems dont impact the test.













In SharePoint Designer, locate the page which you want to add the code-behind to and open in Code-view. For this tutorial I selected the Default page and selected "Create New from Existing". I then saved my new page as CodeBehindPage.aspx and opened the page in code view.

Notice the top section of the page should look like:
<%@ Page language="C#" MasterPageFile="~masterurl/default.master"

Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage,Microsoft.SharePoint,Version=12.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint,
Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint,
Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

Switch to Design view and add a ASP.NET button to the form.
Change the ID of the button to cmdHello.
  
 
 
 
 
 
 
 
 
 
 
 
 Add a ASP.net Label to the form and change the ID to lblMessage.


 
 
 












Save the page and view the page in browser to ensure everything is working.
 
Now, lets add some custom code.
 
Start a new Visual Studio project.
I used VS 2008 and created a .Net 3.5 Class library project.

 
 
 
 
 
 
 
 
 
 
 
 
 
Rename Class1.cs to Messenger.cs
 
using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyHelloWorld
{
   public class Messenger
   {
   }
}



Add references to:
System.web
Microsoft.SharePoint
 
Add the following to your class:
using Microsoft.SharePoint;

using System.Web.UI;
using System.Web.UI.WebControls;

change:
public class Messenger
to
public partial class Messenger: System.Web.UI.Page

Add code to your class so that you have the following:
using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyHelloWorld
{
public partial class Messenger: System.Web.UI.Page
{
  protected Button cmdHello;
  protected Label lblMessage;
  protected override void OnInit(EventArgs e)
  {
   base.OnInit(e);
   cmdHello.Click += new EventHandler(cmdHello_Click);
  }

  protected void Page_Load(object sender, EventArgs e)
  {
   if (!IsPostBack)
   {
    lblMessage.Visible = false;
    cmdHello.Text = "Say Hello";
   }
  }

  protected override void OnLoad(EventArgs e)
  {
   base.OnLoad(e);
  }

  protected void cmdHello_Click(object sender, EventArgs e)
  {
   lblMessage.Text = "Hello";
   lblMessage.Visible = true;
  }
 }
} 

Sign assembly with strong-name-key

Build assembly

Copy assembly to C:\inetpub\wwwroot\wss\VirtualDirectories\80\bin where 80 is the port number of you web application.

OK, so now we have our page ready, we have our code ready, and we need to tell our custom page to use the new code instead of the native SharePoint code.
 In SharePoint Designer, open the page in code view and replace:
<%@ Page language="C#" MasterPageFile="~masterurl/default.master"
Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage,Microsoft.SharePoint,Version=12.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" meta:webpartpageexpansion="full" meta:progid="SharePoint.WebPartPage.Document" %>
With:
<%@ Page language="C#" MasterPageFile="~masterurl/default.master" Inherits="MyHelloWorld.Messenger" meta:progid="SharePoint.WebPartPage.Document" %>

Find the public key token of your assembly. You can do this through Visual Studio or by using the RedGate Reflector tool.



















Add the following to the safecontrols section in the web.config file:
(the web.config file is located in C:\inetpub\wwwroot\wss\VirtualDirectories\80 where 80 is the port number of your web application)

<SafeControl Assembly="MyHelloWorld, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0c51afea590bb15c" Namespace="MyHelloWorld" TypeName="*" Safe="True" AllowRemoteDesigner="True" />

Save the page and the web.config file and browse to the page.

You will see that the custom code now works !!




















Now the fun part...
What if the namespace in my class has a different name than the assembly... where do I use the assembly name vs. namespace name?
Go back to your code in Visual Studio and change the namespace in the class to MyHelloWorldfromCompanyX and rebuild the solution.

Copy the new assembly to C:\inetpub\wwwroot\wss\VirtualDirectories\80\bin and select to override the old one.

If you now try to browse to your custom SharePoint page you will get the following error:









So, to resolve this we need to open the custom SharePoint page in code view in SharePoint designer.

Then, change
<%@ Page language="C#" MasterPageFile="~masterurl/default.master" Inherits="MyHelloWorld.Messenger" meta:progid="SharePoint.WebPartPage.Document" %>

to
<%@ Page language="C#" MasterPageFile="~masterurl/default.master" Inherits="MyHelloWorldfromCompanyX.Messenger" meta:progid="SharePoint.WebPartPage.Document" meta:webpartpageexpansion="full" %>

Save the page and open the page in web browser...you can expect the following error:






So, we also need to change the namespace in the web config file

Change:
<SafeControl Assembly="MyHelloWorld, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0c51afea590bb15c" Namespace="MyHelloWorld" TypeName="*" Safe="True" AllowRemoteDesigner="True" />
to
<SafeControl Assembly="MyHelloWorld, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0c51afea590bb15c" Namespace="MyHelloWorldfromCompanyX" TypeName="*" Safe="True" AllowRemoteDesigner="True" />

Save the web.config file and refresh the browser window....and... wala...your custom code is working !!

Enjoy !!

0

How to restore only a single SharePoint Site Collection from a full SharePoint Server Farm backup

Recently someone asked me how to restore only a single site collection from a full SharePoint FARM backup.


I did a bit of testing and it really is a simple process.
Herewith a simple tutorial on how to use the stsadm command line to achieve this.


1-Create two site collections (test site A and test site B) and then perform a full farm backup.












2-You will see that a new backup folder and xml file was created.











3-Upload a document to test site A:




















4-Upload a document to test site B:























5-Open the command prompt and browse to the 12 hive folder.


6-You can now use the STSADM command line tool to inspect the structure (tree) of the  full backup.
 * Note that this will not perform a restore but rather only show you what the backup  consist of and which items can be selected for restore.

Execute the following:
C:\Program Files\Common Files\microsoft shared\Web Server Extensions\12\BIN>stsadm -o restore
-directory "C:\test sp backups" -url http://localhost:40513/ -restoremethod overwrite -showtree

You will see the backup structure and receive a note that the -showtree parameter needs to be removed before the restore can take place. below is a snippet from the results:
Restore list.

Farm\
[SharePoint_Config]\
Windows SharePoint Services Web Application\
SharePoint - 80\
WSS_Content\
SharePoint - 40513\
WSS_Content_94a95d18ddbf428592f9f8c6b4070b9c\
[WSS_Administration]\
[Web Application]\
[SharePoint_AdminContent_4a553eae-d295-4895-a261-83e8849e3deb]\

[ ] - item cannot be selected.
* - not selected to be restored.


To start the backup or restore process, run the command again omitting the -showtree option.

7-You can now run the stsadm command again, but this time change add the -item parameter and provide the full path of the site collection which you want to restore and remove the -showtree param.

C:\Program Files\Common Files\microsoft shared\Web Server Extensions\12\BIN>stsadm -o restore -directory "C:\test sp backups" -item "Farm\Windows SharePoint Services Web Application\SharePoint - 40513" -url http://localhost:40513/ -restoremethod overwrite


Warning: All selected items will be overwritten. Do you want them to be overwritten (y/n)? y
 
You will be prompted for a username and password - provide site admin credentials.
 
The restore should run and you should see the results when done:
Verbose: The IIS Web Site has been extended. You may need to use iisreset.exe to restart IIS before your site becomes accessible.


Finish Time: 2/9/2010 8:08:01 PM.
Progress: Completed
Verbose: The backup/restore process included the following objects:
*Farm\
*[SharePoint_Config]\
*Windows SharePoint Services Web Application\
*SharePoint - 80\
*WSS_Content\
SharePoint - 40513\
WSS_Content_94a95d18ddbf428592f9f8c6b4070b9c\
*[WSS_Administration]\
*[Web Application]\
*[SharePoint_AdminContent_4a553eae-d295-4895-a261-83e8849e3deb]\


Completed with 0 warnings.
Completed with 0 errors.
Restore completed successfully.
-------------------------------------------------
Operation completed successfully.
 
8-When you now go back to your 2 test sites, you will see that only site B has been restored.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Please note that I did not test this on a farm which contains multiple web apps.
You also will need to SharePoint Admin account to be running.
I did not test this thoroughly so please check before you run this on a production environment !!
 
For further options and guidance please look at: http://technet.microsoft.com/en-us/library/cc262087.aspx
 
Enjoy !!!