Question

I am trying to implement the following: When a user opens the list view, and the URL contains ?SelectedID=<ListItemID> - select this list item in the list view.

I have the code working just fine, but only when at least one item is already selected in the list. When none of the list items are selected GetCurrentCtx() returns null.

When GetCurrentCtx() returns null it means "list is not selected" or the context is not in the list, that's understandable. But how do we force the context to point to the list? Is there a way to do it? Any help would be greatly appreciated.

(function(){
ExecuteOrDelayUntilScriptLoaded(selectElement, "core.js");
function selectElement()
{
    var currentItemId = getQueryString('SelectedID')
    console.log(currentItemId);
    var currentUserAdmin = SP.PageContextInfo.get_webPermMasks().has( SP.PermissionKind.managePermissions);
    if(currentUserAdmin){
        var ctxCur = window["ctx" + GetCurrentCtx().ctxId];
        if (ctxCur){                 
            var ctxId = GetCurrentCtx().ctxId;
            var iid = ctxId + ',' + currentItemId + ',0';
            var row = $("tr[iid='" + iid + "']");
            if (row.length != 0) 
            {
               if (ItemIsCurrentlyVisible(row[0])) {
                   ToggleItemRowSelection2(ctxCur, row[0], true, true);
               }
            }
        }
    }

    function getQueryString  ( field, url ) {
        var href = url ? url : window.location.href;
        var reg = new RegExp( '[?&]' + field + '=([^&#]*)', 'i' );
        var string = reg.exec(href);
        return string ? string[1] : null;
    };
}
})()
Was it helpful?

Solution

This is how we get the context we want. Turns out g_ctxDict object contains an array of all contexts. And I can iterate through them and find the one I need

var ctxCur;
for(var prop in g_ctxDict){

   if(g_ctxDict[prop].listUrlDir.indexOf("/Lists/MystListName") != -1){
      console.log(g_ctxDict[prop]);
      ctxCur = g_ctxDict[prop];
   }
}

The full solution:

(function(){
ExecuteOrDelayUntilScriptLoaded(selectElement, "core.js");
function selectElement()
{
    var currentItemId = getQueryString('SelectedID')

    var currentUserAdmin = SP.PageContextInfo.get_webPermMasks().has( SP.PermissionKind.managePermissions);
    if(currentUserAdmin){
        var ctxCur;
        for(var prop in g_ctxDict){

           if(g_ctxDict[prop].listUrlDir.indexOf("/Lists/Agenda") != -1){
              console.log(g_ctxDict[prop]);
              ctxCur = g_ctxDict[prop];
           }
        }

        //var ctxCur = window["ctx" + GetCurrentCtx().ctxId];
        if (ctxCur){                 
            var ctxId = ctxCur.ctxId;
            var iid = ctxId + ',' + currentItemId + ',0';
            var row = $("tr[iid='" + iid + "']");
            if (row.length != 0) 
            {
               if (ItemIsCurrentlyVisible(row[0])) {
                   ToggleItemRowSelection2(ctxCur, row[0], true, true);
               }
            }
        }
    }

    function getQueryString  ( field, url ) {
        var href = url ? url : window.location.href;
        var reg = new RegExp( '[?&]' + field + '=([^&#]*)', 'i' );
        var string = reg.exec(href);
        return string ? string[1] : null;
    };
}
})()

Update

I've also added this code in order to enable ribbon tabs:

var webPart = $(".ms-webpart-chrome-title:contains('WEB_PART_TITLE')").closest("[id^='MSOZoneCell_WebPartWPQ']")[0]
WpClick({target: webPart});
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top