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.
- First, we use our mad JavaScript skills to make the form fields expand/collapse and start with all fields collapsed.
- Then add two more CEWPs to the page naming one Department A and the other Department B.
- 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.
- 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).
- Power User Toolbox: JavaScript for SharePoint - Pt1
- Power User Toolbox: JavaScript for SharePoint - Pt2
- Power User Toolbox: JavaScript for SharePoint - Pt3
- Power User Toolbox: JavaScript for SharePoint - Pt4
- Power User Toolbox: JavaScript for SharePoint - Pt5
- Power User Toolbox: JavaScript for SharePoint - Pt6
- Power User Toolbox: JavaScript for SharePoint - Pt7
- Power User Toolbox: JavaScript for SharePoint - Pt8
- Power User Toolbox: JavaScript for SharePoint - Pt9