Home > SharePoint > Conditional menu in SharePoint List

Conditional menu in SharePoint List

2012/05/21

With Custom_AddListMenuItems we can create a custom item in the SharePoint menu for a list item, but sometimes this item must be created only for specific items, depending for example from a specific item value.

In this example we assume a List field named “Document Type” from a list named “Documental”, and we want to display a menu “Substitute” only for item with Document Type = “DOCUMENT”.

The problem is that we cannot directly read the item values of custom fields with GetAttributeFromItemTable , we can always traverse the page DOM with JQuery but is a bad idea : it is easy to change a column position.

The solution is to use the Lists.asmx sharepoint service called in synchronous mode, because in async the CamOpt calls are not working in the callback function.

So the code of the CEWP in the view page is

<script type="text/javascript">
    var strUrl;
    var strCtxHttpRoot;
    var strItemUrl;
    var objM;
    
    function Custom_AddListMenuItems(m, ctx) {
        var strID = GetAttributeFromItemTable(itemTable, "ItemId", "Id");
        strCtxHttpRoot = ctx.HttpRoot ;
        objM = m;
        var strSoap = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/"><listName>Documental</listName><query><Query xmlns=""><Where><Eq><FieldRef Name="ID" /><Value Type="Counter">' + strID + '</Value></Eq></Where></Query></query><viewFields><ViewFields xmlns=""><FieldRef Name="Document_x0020_Type" /></ViewFields></viewFields></GetListItems></soap:Body></soap:Envelope>';
        $.ajax({
            url: strCtxHttpRoot + "/_vti_bin/lists.asmx",
            type: "POST",
            dataType: "xml",
            async: false,
            data: strSoap,
            complete: processResult,
            contentType: "text/xml; charset=\"utf-8\""
        });     
    }
    
    function processResult(xData, status) {   
        if (status == "success"){
            $(xData.responseXML).find("z\\:row").each(function() {
                var strDocType = $(this).attr("ows_Document_x0020_Type") ;
                if(strDocType == "DOCUMENT"){
                    var strImagePath = "/intranet/Media/UPDATEPAGEVARIATION.GIF";
                    var strID = GetAttributeFromItemTable(itemTable, "ItemId", "Id");
                    // Add menu item
                    CAMOpt(objM, "Substitute" , "callSubsts();" , strImagePath);
                    CAMSep(objM);
                    return false;                
                }
            });
        }
        else{
            window.document.getElementById("divSpin").style.display = 'none';
            if (xData.responseXML != undefined)
                window.alert(xData.responseXML.text);
            else
                window.alert("Error. call assistance: problem with Custom_AddListMenuItems verifying doctype if DOCUMENT.");            
        }
    }    
    
    function callSubsts(){
        //action
    }
</script>

Advertisements
Categories: SharePoint
%d bloggers like this: