Showing posts with label master page. Show all posts
Showing posts with label master page. Show all posts

Sunday, December 27, 2009

How to get or access master page in user control

My requirement is I want to get the reference to the master page and access the public property of a master page in user control. As we know that master page is also inherited from the user control, I can say this is simple and we can easily get the reference.

I am using master page in my web application and all pages are using that master page. And there are user controls in the application and my requirement is how to access the master page property inside a user control. Below is the solution how I resolved the problem.

In user control ASCX file declare the below line to establish the reference to the master page.

<%@ Reference Control="~/DefaultMaster.Master" %>

IN the ASCX.CS file, we need to fill the master page object to access it's properties. For this,

  • Declare a master page class variable as the user control class variable as shown below.

DefaultMaster masterpage = null;

NOTE: Remember the DefaultMaster is the class of DefaultMaster.master class.

  • In Page_Load event of user control, fill the masterpage object as shown.

masterpage = this.Page.Master as DefaultMaster;

Now, you are all set to use master page object and access everything inside it. If you observed, everything is simple object model. I am just accessing the objects. I think, you got better idea and the way I used. Hope this helps and any comments always welcome.

Tuesday, May 26, 2009

Adding master page to a SharePoint web page

In SharePoint, when we add a new page from browser by going to Site Actions and select Create Page then there we will select the page layout and create the page. So we are selecting the page layout, its always binds to a master page. Whenever you change the default or custom master page, then the changes will be applied to all the page layouts by default. Because SharePoint won’t give direct master page url or name any where. it will be ~masterurl/custom.master for custom master page and ~masterurl/default.master for the default master page.

But when we add a new ASPX page from the SharePoint designer, there we are not having any option to select page layout and inherit it.  So, we will create a simple ASPX page where nothing is added by default to the page other than the head, body tags. When we try to add the master page to the current page then we need to follow some steps to apply master page to the current page. Below are the steps to follow to add master page to the aspx page we just added.

  • By default you will create the ASPX page in pages folder if your site is using publishing site template otherwise create page in the root of the web site.
  • You can create site by right click on the pages folder or on the root site and then select New –> ASPX page. The empty ASPX page when we add will look like as shown below.
  • Empty_ASPX_Page_No_Master
  • We need to perform below steps to add master page to the current page.

1. Add master page reference to the page.

2. Add Placeholders required to the page depends on the master page.

1. Adding master page reference to the page.

  • Now, we need to add the reference to the master page. We can do this by adding below tags.
  • For publishing site template pages, we need to add two entries.
  1. reference master page

    <%@ Reference VirtualPath="~masterurl/custom.master" %>

  2. Adding <%@ Page tag which will inherits from the publishing page.

    <%@ Page language="C#" Inherits="Microsoft.SharePoint.Publishing.PublishingLayoutPage,Microsoft.SharePoint.Publishing,Version=12.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" meta:progid="SharePoint.WebPartPage.Document" %>

  • But for team web site we need to add page directive as follows.

    <%@ Page language="C#" MasterPageFile="~masterurl/default.master"    Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage,Microsoft.SharePoint,Version=12.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" meta:progid="SharePoint.WebPartPage.Document" %>

2. Add Placeholders required to the page depends on the master page.

  • This is the very crucial and important step we need to perform. Because placeholders are not same for all site templates. This again will depends on the master page as well. If you customized the master page then you need to add placeholders depends on the placeholders you define.
  • Here i will tell you the main or default placeholders we need to add to a page.

    <asp:Content ContentPlaceHolderId="PlaceHolderPageTitle" runat="server">
    </asp:Content>
    <asp:Content ContentPlaceHolderId="PlaceHolderPageTitleInTitleArea" runat="server">
    </asp:Content>
    <asp:Content ContentPlaceHolderId="PlaceHolderTitleAreaClass" runat="server">
    </asp:Content>
    <asp:Content ContentPlaceHolderId="PlaceHolderPageDescription" runat="server">

    </asp:Content>
    <asp:Content ContentPlaceHolderId="PlaceHolderBodyRightMargin" runat="server"> </asp:Content>

    <asp:Content runat="server" ContentPlaceHolderID="PlaceHolderMain"></asp:Content>

Placeholder “PlaceHolderPageTitle” is for setting the page title of the current page.

Placeholder “PlaceHolderMain” is the main and important placeholder where we keep all the content on the page. Place all the content of your web page in this placeholder.

Note: This blog post is only for letting you know about how to convert sample ASPX page to SharePoint web page by applying the master page to it. All the placeholders and the content is not 100% match your master page. If you have any problems in applying any step then please provide a comment, so that i will look into it.

Monday, April 27, 2009

Ajax,Master page problem in Sharepoint 2007 [MOSS]

Problem:
Ajax is not working as expected in SharePoint 2007.

Solution:
1. First we need to install Ajax extensions on the SharePoint server.
2. After installed Ajax, We need to add the corresponding entries in web.config file to know the web application that the script manage, ajax requests etc... For this we need to change the configuration.
3. Copy all the ajax dlls to GAC and then follow the instructions below.

There are plenty of problems while integrating Ajax into SharePoint when i was very new to SharePoint. I solved those problems after a long research on Masterpage and Java scrpt.

Here are the steps i followed.
Before change any of the file as mentioned below please take the backup of every file and save it in some safe location.

When i was new to learn SharePoint, i implemented this. I am not completly sure that we need to change the authorizedTypes Tag in web.config of my site ot not, but it works.
The changed section looks like below.

Web.Config changes

<authorizedTypes>
<authorizedType Assembly="System.Workflow.Activities, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Workflow.*" TypeName="*" Authorized="True" />
<authorizedType Assembly="System.Workflow.ComponentModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Workflow.*" TypeName="*" Authorized="True" />
<authorizedType Assembly="System.Workflow.Runtime, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Workflow.*" TypeName="*" Authorized="True" />
<authorizedType Assembly="System.Transactions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Namespace="System*" TypeName="*" Authorized="True" />
<authorizedType Assembly="System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Namespace="System*" TypeName="*" Authorized="True" />
<authorizedType Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Namespace="Microsoft.SharePoint.Workflow" TypeName="SPWorkflowActivationProperties" Authorized="True" />
<authorizedType Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Namespace="Microsoft.SharePoint.Workflow" TypeName="SPWorkflowTaskProperties" Authorized="True" />
<authorizedType Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Namespace="Microsoft.SharePoint.Workflow" TypeName="SPWorkflowHistoryEventType" Authorized="True" />
<authorizedType Assembly="Microsoft.SharePoint.WorkflowActions, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Namespace="Microsoft.SharePoint.WorkflowActions" TypeName="*" Authorized="True" />
<authorizedType Assembly="System.Workflow.Activities, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Workflow.*" TypeName="*" Authorized="True" />
<authorizedType Assembly="System.Workflow.ComponentModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Workflow.*" TypeName="*" Authorized="True" />
<authorizedType Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Namespace="Microsoft.SharePoint.Workflow" TypeName="SPWorkflowActivationProperties" Authorized="True" />
<authorizedType Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Namespace="Microsoft.SharePoint.Workflow" TypeName="SPWorkflowTaskProperties" Authorized="True" />
<authorizedType Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Namespace="Microsoft.SharePoint.Workflow" TypeName="SPWorkflowHistoryEventType" Authorized="True" />
<authorizedType Assembly="Microsoft.SharePoint.WorkflowActions, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Namespace="Microsoft.SharePoint.WorkflowActions" TypeName="*" Authorized="True" />
<authorizedType Assembly="mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Namespace="System" TypeName="Guid" Authorized="True" />
<authorizedType Assembly="mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Namespace="System" TypeName="DateTime" Authorized="True" />
<authorizedType Assembly="mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Namespace="System" TypeName="Boolean" Authorized="True" />
<authorizedType Assembly="mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Namespace="System" TypeName="Double" Authorized="True" />
<authorizedType Assembly="mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Namespace="System" TypeName="String" Authorized="True" />
<authorizedType Assembly="mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Namespace="System.Collections" TypeName="Hashtable" Authorized="True" />
<authorizedType Assembly="mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Namespace="System.Collections" TypeName="ArrayList" Authorized="True" />
<authorizedType Assembly="mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Namespace="System.Diagnostics" TypeName="DebuggableAttribute" Authorized="True" />
<authorizedType Assembly="mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Namespace="System.Runtime.CompilerServices" TypeName="CompilationRelaxationsAttribute" Authorized="True" />
<authorizedType Assembly="mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Namespace="System.Runtime.CompilerServices" TypeName="RuntimeCompatibilityAttribute" Authorized="True" />
<authorizedType Assembly="mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Namespace="System" TypeName="Int32" Authorized="True" />
<authorizedType Assembly="mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Namespace="System" TypeName="TimeSpan" Authorized="True" />
<authorizedType Assembly="mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Namespace="System.Collections.ObjectModel" TypeName="Collection`1" Authorized="True" />
</authorizedTypes>

Master page changes:
Second, i removed the onload function from BODY tag. This was causing problem, i am not sure that whether we need to remove or not!!! But it's working as expected.

I removed following section from BODY of master page.
onload="javascript:if (typeof(_spBodyOnLoadWrapper) != 'undefined') _spBodyOnLoadWrapper();"

JAVASCRIPT CHANGES:
Third, Go to init.js file in 12\TEMPLATE\LAYOUTS\1033 folder.
find the method spFormOnSubmitWrapper()
in this function, just remove the line return false; in
if (_spFormOnSubmitCalled) statement;
this is because, if that page is already posted back, sharepoint won't post it back again.
but to work AJAX calls, we need to disable it. Because Ajax sends the XmlHttpRequests many times depends on user requests, SharePoint ignore all the calls except the very first call. This is the reason when very first time you integrate Ajax into SharePoint the first call always successful and from the second call no response.
Hope it helps.