User Tools

Site Tools


notes:sharepoint2010:javascript

JavaScript and SharePoint 2010

Display a modal dialog box (example #1):

var url = 'http://mysite/_layouts/mypage.aspx';
var options = {url: url, width: 800, height: 650, allowMaximize: false, title: 'My Page', showClose: true};
var dialog = SP.UI.ModalDialog.showModalDialog(options);

Display a modal dialog box (example #2):

function OpenModalDialog(url) 
{
    options = SP.UI.$create_DialogOptions();
    options.width = 800;
    options.height = 650;
    options.allowMaximize = false;
    options.title = 'My Popup';
    options.showClose = true;
    options.url = url;
    options.dialogReturnValueCallback = Function.createDelegate(null, CloseCallback);
 
    SP.UI.ModalDialog.showModalDialog(options);
}
 
function CloseCallback(result, target)
{
    SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK);
}

Verify that only one item has been selected:

var items = SP.ListOperation.Selection.getSelectedItems();
var count = CountDictionary(items);
if (count != 1)
{
  alert('More than one item or no items have been selected. Please select exactly one item.');
}
else
{
  var itemId = items[0].id;
  // ...
}

Create a list item:

function addListItem(listTitle)
{
    try
    {
        // Get the current context.
        var context = SP.ClientContext.get_current();
 
        // Get the current website.
        var web = context.get_web();
 
        // Get the collection of lists.
        var lists = web.get_lists();
 
        // Get the list.
        var list = lists.getByTitle(listTitle);
 
        // Create the IteamCreationInformation.
        var info = new SP.ListItemCreationInformation();
 
        // The addItem takes the itemCreationInformation object and returns the listItem.
        var item = list.addItem(info);
 
        // Set the list item values.
        item.set_item('Title', 'New item');
        // ...
 
        // Update the listItem.
        item.update();
 
        // Execute the operation passing two callbacks OnSucceed and OnFailure.
        context.executeQueryAsync(Function.createDelegate(this, this.onSucceeded), 
            Function.createDelegate(this, this.onFailed));
    }
    catch (e)
    {
        alert('error:' + e.Message);
    }
}
 
function onSucceeded()
{
    alert('The item has been added successfully.');
}
 
function onFailed(sender, args)
{
    alert('The item has not been added. Reason: ' + args.get_message());
}

Determine the content type of the current list as well as the parent content type. This code can be used in a ribbon:

var contentType;
var parentContentType;
 
function getContentType()
{
    var ctx = new SP.ClientContext.get_current();     
    var web = ctx.get_web();
    var listId = SP.ListOperation.Selection.getSelectedList();
    var list = web.get_lists().getById(listId);
    var item = list.getItemById(items[0].id);
    contentType = item.get_contentType();
    ctx.load(contentType);
    ctx.executeQueryAsync(onSuccess, onFailure);              
}
 
function onSuccess()
{
    var name = contentType.get_name();
 
    parentContentType = contentType.get_parent();
    var ctx = new SP.ClientContext.get_current();     
    var web = ctx.get_web();
    ctx.load(parentContentType);
    ctx.executeQueryAsync(onSuccess2, onFailure);
 
    return true;
}
 
}
 
function onFailure()
{
    return false;
}

Location of Debug versions of JS scripts (e.g. SP.Core.debug.js, SP.Ribbon.debug.js):

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS

Force SharePoint to use debug JS scripts by modifying web.config:

1. Open the web.config located at %inetpub%\wwwroot\wss\VirtualDirectories\80
2. Add <deployment retail="false" /> to the system.web section
notes/sharepoint2010/javascript.txt · Last modified: 2015/06/24 by admin