Friday, May 02, 2014

Remove Quick Launch Node from Recent when you create a List using REST from SharePoint App

This is a simples article on how we can use REST to manage Navigation QuickLaunch using REST call in a SharePoint App Base. 

No, I didn't find property or option to disable the List from appearing in the annoying “Recent” Group on the Left Menu “Quick Link”. If there is I will be very happy to know also.
What i did was use REST calls to manage the NavigationNodes "just to be clear".

This is my case: 
I want to create a support List in HostWeb using SharePoint App with some parameters for administrative purpose of my Solution and for that type of need, end users and Site admin should not see this list,  
When the list was prepared to be created as hidden and QuickLaunch as False.
Then you create the list using REST that should appear Hidden and what happen..... The List appears in the Left Menu as a huge traffic light...



The code bellow is used to create a List using REST in HostWeb:

function AddListHostWeb(appWebUrl, hostWebUrl) {
    var executor = new SP.RequestExecutor(appWebUrl);
    executor.executeAsync({
        url: appWebUrl + "/_api/SP.AppContextSite(@target)/web/lists?@target='" + hostWebUrl + "'",
        method: "POST",
        body: "{ '__metadata': { 'type': 'SP.List' }, 'AllowContentTypes': true, 'BaseTemplate': 100,'ContentTypesEnabled': true, 'Description': 'User Custom Actions Recycling Bin', 'Title': 'MyCustomList','Hidden':'True', 'OnQuickLaunch': false  }",
        headers: { "content-type": "application/json;odata=verbose" },
        success: function () {
            GetDeleteQuicklaunch(appWebUrl, hostWebUrl);
        }, error: function (xhr) { SP.UI.Notify.addNotification(xhr.status + ': ' + xhr.statusText, false); }
    });
}

Some Microsoft Link that can support your:
http://msdn.microsoft.com/en-us/library/office/dn531433(v=office.15).aspx

Ok the easy way "Out of the Box" to resolve this issue is accessing the Quick Launch and remove the option that was added, but if you are creating a solution this should be automatic... i think.



To don't use this option and make using custom code in the moment of the Creation of the List i create this example by REST to remove the Navigation Node.


Remove QuickLink option using REST in SharePoint App

The following code gets the quick Launch ID associated with the created List and calls the DELETE Method to remove the node from Recent group.

To remove the Navigation node  was created the following JSOM Methods
  • GetDeleteQuickLaunch
    • This Method Get the Navigation node ID  using Rest associated with the created List and call the Delete method 
    • The REST Call "...web/navigation/quicklaunch?$expand=Children..." returns Root navigation nodes and their children "to get the node from RECENT"
  • DeleteQuickLaunch
    • Send a REST call to delete the Navigation Node.

Here is the code where the call is made to remove the Navigation link from the Quick Launch.

function GetDeleteQuicklaunch(appWebUrl, hostWebUrl) {
    var executor = new SP.RequestExecutor(appWebUrl);
    executor.executeAsync(
    {
        url: appWebUrl + "/_api/SP.AppContextSite(@target)/web/navigation/quicklaunch?$expand=Children&$select=Title,Children/Title,Children/Id&@target='" + hostWebUrl + "'",
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {
            var jsonObject = JSON.parse(data.body).d;
            for (var i = 0; i < jsonObject.results.length; i++) {
                if (jsonObject.results[i].Children.results.length > 0) {
                    var children = jsonObject.results[i].Children;
                    for (var x = 0; x < children.results.length; x++) {
                        if (children.results[x].Title == 'MyCustomList') {
                            DeleteQuicklaunch(appWebUrl, hostWebUrl, children.results[x].Id)
                        }
                    }
                }
            }
        },
        error: function (xhr) { SP.UI.Notify.addNotification(xhr.status + ': ' + xhr.statusText, false); }
    });
}

function DeleteQuicklaunch(appWebUrl, hostWebUrl, id) {
    var executor = new SP.RequestExecutor(appWebUrl);
    executor.executeAsync(
    {
        url: appWebUrl + "/_api/SP.AppContextSite(@target)/web/navigation/quicklaunch('" + id + "')?@target='" + hostWebUrl + "'",
        method: "DELETE",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function () { },
        error: function (xhr) { SP.UI.Notify.addNotification(xhr.status + ': ' + xhr.statusText, false); }
    });
}


The main idea of the code is get the QuickLink Node id and send a REST request to delete the item.
When you are using Fiddler you are able to get the NavigationNodes from QuickLaunch as shown in the image.



After this change the Link should not be visible anymore in the left menu and continues hidden to users.



Of course even if the list is Hidden you are able to access using direct Url to the List....



Simple and done...
Maybe i am missing something in the creation of the list or other thing "i am using JSOM", if you know a workaround or property, please provide some feedback, here are the properties of the List.

Hope you enjoy this article.
Kind regards, 
Andre Lage
Post a Comment