Skip Ribbon Commands
Skip to main content

Tanmay Shahane | zevenseas | SharePoint Blog

:

Tanmay Shahane | zevenseas | SharePoint Blog > Posts > Restricting web part adding on page for specific zones with wonderful SharePoint JavaScripting
August 20
Restricting web part adding on page for specific zones with wonderful SharePoint JavaScripting

 

Hello kind readers…
 
At times comes situation when you are building a large scale application (often it comes right :P) and there are a lot of pages based on layouts and requirements and what not and on top of that there could be a LARGE number of end users involved in adding web parts on the pages with some specific layouts… now of course you would want to make sure that no body misses out the consistency and that web parts are added at the right zone and all plus only the indented web part are added on the layout…
 
Now how do you confirm that and make sure it all remains consistent and friendly for the user…
Now SharePoint has some fantastic JavaScripting inside the box and have loads of options which can be leveraged to our benefit… Now lets see how it works…
 
When you click on that "edit web part page" button the page goes in edit mode with webpart zones showing up, nothing fancy, then you click the "Add a webpart" link on the zones.. This is when the WPADDER.JS is loaded on the page…. But how does that help you.. Hmmm glad you asked…lets dive in some code to see how things would work for you… on your custom JS file or master page or whatever your choose we need to add this code…

 

//to make sure your code is only called after the respective file is loaded

 

ExecuteOrDelayUntilScriptLoaded(someFunc,"wpadder.js");
 
Then define the someFunc (could be any name for the function)
 
Function someFunc()
{
//get the add button from the web part adding panel
Var btnAdd = $("div.ms-wpadder-buttonArea button:first-child");
//remove the inline click event handler
btnAdd.removeAttr('onclick');
//juice it up with your own event handler using the sweetest weapon of all JQUERY!!
btnAdd.click(function() {
//get the webpart of object, this comes from the wpadder.js, where the WPAdder object instance is found giving all //properties on the selected webpart item
var selectedWp = WPAdder._getSelectedItem();
//using the same object instance you can get the selected zone id, the zone you have selected for adding the webpart
Var zoneId = WPAdder._getZoneSelect().value
  //you can check the title of the selected webpart for whatever name you want
  //u can also use id of the web part
   if(selectedWp.title == 'Content Query')
  {
     //if the webpart is the one you don't want user to add in specific location, check the zone next
     If(zoneId == 2)
     {
       //if the zone is not where the web part should be, give the message and return
        alert('not allowed here');
        return false;
      }  
   }
    else
    {
           //else if all is correct just let the webpart be added to the page gracefully!!
           WPAdder.addSelectedItemToPage();
           return false;
     }
 
 });
}
 
This is all you need to have the checks in place. Now this functionality can be used in many ways, you can also stop users for adding the web part to the page at all if its not some specific page or site, to have some validations. Conditions can be any depending on your requirement.
 
This is just an example of how you can use the inbuilt libraries of SharePoint with combinations of your preferred JS libraries to give a very flexible user experience. There are many more things you can do with the web parts on client side but m not going to talk about all right now but I hope this give you a direction… and if you already know about this then its already cool :) … thanks anyways for hitting this road…and hope this helps someone!
 
- "T"

 

Comments

nice trick

Nice one mate, just one quick question, if at all there is an exception of allowing a user to add some specific wepart irrespective of the layout provided, how we handle that situation, meaning having the validation in place but some control for exceptional scenarios.
 on 8/20/2011 7:22 PM

@ Above

First of all, thanks for reading the post..

Second... m not very sure if I understood your question correctly, in my example I have used a layout related scenario but as far as you know the title or Id of the webpart you can put in any kinda specific validations.. so what I mean is its not dependent on the page layout at all... as I said.. you can make it limtited to a particular page or site as well.. depends on how you want to put in place. Hope I got that correct... feel free to question more if I haven't understood and answered correctly.

PS- This does not yet cover the situation where the user can add a webpart to a specific allowed zone and then move it to another by drag drop. (That some other time.. :) )

         - "T"
Tanmay Shahane on 8/20/2011 10:52 PM

How to get webpart Id

Hi Tanmay,

How do I get id of selectedWp. selectedWp.id gives me different id on different servers.

selectedWp.Title does not work for me as we support multiple languages.

Regards,
Ojas
http://ojasmaru.blogspot.in/
 on 4/17/2012 11:38 PM

Hi Ojas

Sorry for the late reply, but can you tell me what exactly are u getting as ID, there should be some common value to it which can be used across servers.
Tanmay Shahane on 5/4/2012 7:05 AM

Christian Louboutin Boots On Sale


My partner and i take a nap, please take a straw head wear, along with buckles face,desire grass inside Hong Kong. I came to be to depart you, Once you enter in the path involving crimson dirt,   Christian Louboutin Boots On Sale   the actual wind chime because i tend not to invest the particular wedding ring.While this individual came up, This individual shook his / her head in a very vast natrual enviroment road, and smiled. I will be myself, you did not maintain myself in a aspiration.
http://www.zamshoes.com/ Christian Louboutin Boots On Sale
http://www.zamshoes.com/categories-pumps.html black christian louboutin pumps sale
http://www.zamshoes.com/categories-boots.html christian louboutin boots on sale
http://www.zamshoes.com/categories-for-men.html  christian louboutin men
http://www.zamshoes.com/categories-sneakers.html christian louboutin sneakers sale
http://www.zamshoes.com/categories-flats.html christian louboutin flats cheap
http://www.zamshoes.com/miu-miu-shoes.html miu miu shoes outlet
http://www.zamshoes.com/jimmy-choo.html jimmy choo shoes on sale
http://www.zamshoes.com/manolo-blahnik.html manolo blahnik something blue
 on 10/24/2012 6:01 PM

restrict adding webparts

hi. we want to restrict adding webpart when the limit of the webparts on a page exceeded for a specific page layout... i have written the code in webpartAdding event which is not firing.. any help pls..
 on 3/22/2013 12:13 AM

Add Comment

Title


Body *


Attachments

 

 Related Posts

 
 

 Statistics

 
Views: 1406
Comments: 6
Tags:SharePoint 2010, JQuery
Published:668 Days Ago