Tag Archive for JavaScript

Custom_AddDocLibMenuItems

SharePoint Short #19

When implementing your own Custom_AddDocLibMenuItems, which allows you to add custom menu items to the ECB of SharePoint items, remember to check for other implementations of this method before your own version.

For example:

function YourImplementationOf_AddDocLibMenuItems(m, ctx, url) {
    if (typeof Custom_AddDocLibMenuItems != 'undefined') {
        var CurrentCustom_AddDocLibMenuItems = Custom_AddDocLibMenuItems;
    }
 
    Custom_AddDocLibMenuItems = function (m, ctx, url) {
        // Custom code to add ECB menu items....
 
        if (CurrentCustom_AddDocLibMenuItems === undefined) {
            return false;
        }
 
        return CurrentCustom_AddDocLibMenuItems(m, ctx, url);
    };
}

By checking if Custom_AddDocLibMenuItems is not Undefined and referencing the method at the start of your YourImplementationOf_AddDocLibMenuItems method, you are able to ensure any other implementation of this method is called once you are finished with it.

To have your YourImplementationOf_AddDocLibMenuItems method initiated when a page loads, add the following:

$(document).ready(function () {
    _spBodyOnLoadFunctionNames.push('YourImplementationOf_AddDocLibMenuItems()');
});

Here we use jQuery’s ready function, to add the custom method YourImplementationOf_AddDocLibMenuItems to a SharePoint array that is processed when the DOM is loaded. What this means is that the method will be executed when the page loads and subsequently the custom overload of Custom_AddDocLibMenuItems is called whenever the ECB is opened.

PeopleEditor in Bound Control

Having problems updating a dynamically created PeopleEditor control on postback when utilising it in a bound repeating control? The following solution might help you.

In the CreateChildControls method of your control create and add a HiddenField control at the same level as the PeopleEditor control you want to update:

peopleEditor = new PeopleEditor();
// Initialise your PeopleEditor
// ...
Controls.Add(peopleEditor);
 
_hiddenField = new HiddenField { ID = "_hiddenField" };
Controls.Add(_hiddenField);

and then in the ItemCommand event (for example) of your bound control, set the value of the HiddenField control to the value of the PeopleEditor:

_hiddenField.Value = peopleEditor.CommaSeparatedAccounts.Replace(",", "; ");

Here we replace the , with a ; as this is the separator used within the page DOM by the people editor.
Read more

Dynamically load JavaScript file

Sometimes you don’t always want a JavaScript file to load. If, for instance, a control that utilises it is loaded by a page that already references the same JavaScript file, you wouldn’t want to load the script file again.

Note: This being a SharePoint blog, the script shown here is tailored for that platform but will quite easily port to other non SharePoint sites.

A good example of this would be a custom SharePoint WebPart that uses jQuery. Now, say you don’t have control over the page that will host the WebPart and therefore can’t guarantee that the jQuery script will be present in the page\master page\delegate header control. You could dynamically load the script file as the WebPart loads. The problem with this is that the rendering page might already be loading the same script.

Also, by loading the jQuery script file, especially in an environment you have little control over (maybe a WebPart you developed, which is used by many different systems and configurations) you could potentially cause a conflict on the use of the $ alias, which jQuery creates by default.
Read more

Unable to set value of the property ‘aSettings’

I developed a page, inheriting from LayoutsPageBase which was being displayed in a modal dialog using JavaScript similar to the following:

<script type="text/javascript">
    function DisplayCustomPopupPage() {
        var options = SP.UI.$create_DialogOptions();
 
        options.url = "http://urltocustompage/page.aspx";
        options.title = "Popup Page";
        options.allowMaximize = false;
        options.showClose = true;
        options.width = 800;
        options.height = 450;
 
        SP.UI.ModalDialog.showModalDialog(options);
    }
</script>

The DisplayCustomPopupPage function would be called from a click event on the page.

The popup page had an InputFormTextBox control on it that was configured for rich text mode.

The problem I encountered with this was a JavaScript error that said ‘null’ is null or not an object or Unable to set value of the property ‘aSettings’: object is null or undefined.
Read more

Hide All Site Content Link

Looking for a straight forward and relatively easy way of hiding the All Site Content link from the quick launch area?

All you need to do is create a feature, manisfest file and user control that contains the script to hide the link.

The feature should ideally be site or web scoped although it doesn’t really matter and depends where you want the link removed from.

Create a new empty element file and replace with the following content:
Read more

Follow

Get every new post delivered to your Inbox

Join other followers: