Monday, April 30, 2012

How to get an Edit Page Link in a custom page with no master page applied.

As per my one of requirements I need to add a Content Editor web part in  in my pop up page which has no master page applied. There is no issue in adding a Content Editor web part but I am not getting Site Action option as no master page applied in this page. So in order to edit Content Editor web part in this popup page I need a link on which click users would be able to edit it. At last I came up with this following steps:

1.) Actually whenever we click on Site Action --> Edit Page Button, SharePoint call an inbuilt Java Script function named "MSOLayout_ToggleLayoutMode();". So what we all need to do to just call this Java Script function on our button (in my case) click.

<asp:LinkButton runat="server" OnClientClick="javascript:MSOLayout_ToggleLayoutMode();"          ID="btnEditSaveMode" Text="Edit Page"></asp:LinkButton>

2.) After this you will be able to edit page but its display will be totally different. It is because SharePoint uses its own style for managing display of web part or page edit mode. Now we need to add SharePoint default style sheet in our page too i.e. "sacore.css".

 <link href="/_LAYOUTS/sitedefname(solutionfoldername)/sacore.css" rel="stylesheet" type="text/css" />

3.) Now we are able to edit page and that too in proper format. But we need to set the text of link button as per page status. when Edit Page is clicked it should be changed to Save Page. So for this we need to add a hidden fields which is able to maintain the link button click status. In order to add this I have created a Java Script function which calls  "MSOLayout_ToggleLayoutMode();" function in it and also sets value in hidden field. 


a.)First add a Hidden Field in the page.aspx :

 <asp:HiddenField runat="server" ID="hdnMode" Value="Edit" />

b.) Now write this java script function :



// Set Hidden field value for edit and save page mode.
        function ModeSetter() {
            var Mode = document.getElementById('<%=hdnMode.ClientID %>').value;
            MSOLayout_ToggleLayoutMode();
            if (Mode == "Edit") {
                document.getElementById('<%=hdnMode.ClientID %>').value = "Save";
            }
            else {
                document.getElementById('<%=hdnMode.ClientID %>').value = "Edit";
            }
}

c.)Now I am calling this function on button client click. So code will be  :


<asp:LinkButton runat="server" OnClientClick="javascript:MSOLayout_ToggleLayoutMode();"          ID="btnEditSaveMode" Text="Edit Page"></asp:LinkButton>

4.)But this value will not be get reflected because this in built java script method call page load function which reset the text to previous button. So we need to reset the text again on Page_LoadComplete. We need to write this code at our page.aspx.cs

 protected void Page_LoadComplete(object sender, EventArgs e)
        {
          if (hdnMode.Value == "Edit")
        {
              btnEditSaveMode.Text = Save Page;
           }
          else if (hdnMode.Value == "Save")
           {
              btnEditSaveMode.Text = Edit Page;
          }
       } 


5.) Now you want to display this button only to Admin Users. For that I called a function on page load and checked current User's group and displayed it accordingly. So just put this on your page.aspx.cs Page_Load. 

if (CheckPermission(objSPWeb))
                        {
                            btnEditSaveMode.Visible = true;
                        }
                        else
                        {
                            btnEditSaveMode.Visible = true;
                        } 

*CheckPermission is my own custom method. 
  

No comments:

Post a Comment