1,804 articles and 15,520 comments as of Monday, December 27th, 2010

Thursday, December 11, 2008

Power User Toolbox: JavaScript for SharePoint – Pt8

Conditional Scripts

Sometimes you only want scripts to load for certain users.  Adding Audience Targeting to your custom Content Editor Web Part (CEWP) helps by wrapping your script with SharePoint security trimming.

Divide and Conquer

For example, let’s say I have a very long form used by several departments.  Department A only fills in 3 of the 5 fields while Department B only completes the other two.

  1. First, we use our mad JavaScript skills to make the form fields expand/collapse and start with all fields collapsed.
  2. Then add two more CEWPs to the page naming one Department A and the other Department B.
  3. On the Department A web part, set the Audience Targeting to the ‘Department A’ SharePoint group (here’s an article about setting up Audience groups and configuring Audience Targeting on your web part).  Then do the same for Department B’s web part.
  4. In the respective department web part, add script to expand the particular sections of the form each department uses.

Beware the Stranger

The JavaScript API may not work for anonymous users.  If you use Audience Targeting to only load for authenticated users, the page will avoid errors for users that did not log in.

A Table for One Can Be Fun

In the same vein as Greasemonkey, you can tailor your SharePoint experience to fit your needs with a little scripting.  Only, with this approach your scripts stay on the server pages instead of your browser.  Just remember to set the Audience Targeting on your CEWP to an audience of one.

Admin Back Door

I often try to make my pages “silly proof.”  When I work on requirements like in part 4,  I used to find myself saying, “Why would you change the pre-filled option on the form?  I made it pre-filled for a reason!”  Alas, users can act silly.  I added the switchBack function and a redirect script in case the query string value is undefined (meaning someone guessed the form URL or deleted the query string parameter).  The redirect looks like this:

function setLookupFromFieldName(fieldName, value) {
  if (value == undefined) window.location = "/Lists/Tasks";
  var theSelect = getTagFromIdentifierAndTitle("select","Lookup",fieldName);
...

However, that causes a problem when I’m working on the form as an administrator.  With Audience Targeting I can create a back door for myself.  I place the function I need to overwrite in another CEWP after the one for everyone else.  Then I set the Audience Targeting to the admins group.  (In the example of the redirect script, just remove the second line from the function).

Paul Grenier

View all entries in this series: PaulGrenier-Power User Toolbox»
 

Please Join the Discussion

2 Responses to “Power User Toolbox: JavaScript for SharePoint – Pt8”

Trackbacks

Check out what others are saying about this post...
  1. SharePoint Kaffeetasse #98…

    Tools und Addons Custom Content Editor Web Part for SharePoint jQuery http://jquery.com/ jQuery is a…

  2. SharePoint Kaffeetasse #98…

    Tools und Addons Custom Content Editor Web Part for SharePoint jQuery http://jquery.com/ jQuery is a…




Notify me of comments to this article:


Speak and you will be heard.

We check comments hourly.
If you want a pic to show with your comment, go get a gravatar!