'use strict'
;
var
context;
var
web;
context = SP.ClientContext.get_current();
var
hostweburl;
var
appweburl;
var
appContextSite;
$(document).ready(
function
() {
getUrl();
});
function
getUrl() {
hostweburl = getQueryStringParameter(
"SPHostUrl"
);
appweburl = getQueryStringParameter(
"SPAppWebUrl"
);
hostweburl = decodeURIComponent(hostweburl);
appweburl = decodeURIComponent(appweburl).toString().replace(
"#"
,
""
);
var
scriptbase = hostweburl +
"/_layouts/15/"
;
$.getScript(scriptbase +
"SP.RequestExecutor.js"
, execOperation);
}
function
execOperation() {
var
factory =
new
SP.ProxyWebRequestExecutorFactory(appweburl);
context.set_webRequestExecutorFactory(factory);
appContextSite =
new
SP.AppContextSite(context, hostweburl);
web = appContextSite.get_web();
context.load(web);
context.executeQueryAsync(onGetWebSuccess, onFail);
}
function
onGetWebSuccess() {
console.log(
'Hello '
+ web.get_title());
}
function
onFail(sender, args) {
console.log(
'Failed. Error:'
+ args.get_message());
}
function
AddQuickLaunchLink() {
var
ql = web.get_navigation().get_quickLaunch();
var
nnci =
new
SP.NavigationNodeCreationInformation();
nnci.set_title(
'My Custom Link'
);
nnci.set_url(
'/_layouts/settings.aspx'
);
nnci.set_asLastNode(
true
);
ql.add(nnci);
context.load(ql);
context.executeQueryAsync(
function
() {
$(
'#lblmessage'
).append(
"QuickLaunch link added successfully..."
);
}, onFail);
}
function
AddTopNavicationLink() {
var
TopNav = web.get_navigation().get_topNavigationBar();
var
nnci =
new
SP.NavigationNodeCreationInformation();
nnci.set_title(
'My Custom Link'
);
nnci.set_url(
'/_layouts/settings.aspx'
);
nnci.set_asLastNode(
true
);
TopNav.add(nnci);
context.load(TopNav);
context.executeQueryAsync(
function
() {
$(
'#lblmessage'
).append(
"Top Navigation link added successfully..."
);
console.log(
"TopNav Added"
);
}, onFail);
}
function
RemoreQuickLaunchLink() {
var
ql = web.get_navigation().get_quickLaunch();
context.load(ql);
context.executeQueryAsync(
function
() {
var
e = ql.getEnumerator();
var
notFound =
true
;
while
(notFound && e.moveNext()) {
var
node = e.get_current();
if
(node.get_title() ===
"My Custom Link"
) {
node.deleteObject();
notFound =
false
;
}
}
context.executeQueryAsync(
function
() {
$(
'#lblmessage'
).append(
"QuickLaunch link removed successfully..."
);
console.log(
"QuickLaunch link removed"
);
},
onFail);
},
onFail);
}
function
RemoveTopNavicationLink() {
var
tn = web.get_navigation().get_topNavigationBar();
context.load(tn);
context.executeQueryAsync(
function
() {
var
e = tn.getEnumerator();
var
notFound =
true
;
while
(notFound && e.moveNext()) {
var
node = e.get_current();
if
(node.get_title() ===
"My Custom Link"
) {
node.deleteObject();
notFound =
false
;
}
}
context.executeQueryAsync(
function
() {
$(
'#lblmessage'
).append(
"Top Navigation link removed successfully..."
);
console.log(
"TopNav link removed"
);
},
onFail);
},
onFail);
}
function
getQueryStringParameter(paramToRetrieve) {
var
params =
document.URL.split(
"?"
)[1].split(
"&"
);
for
(
var
i = 0; i < params.length; i = i + 1) {
var
singleParam = params[i].split(
"="
);
if
(singleParam[0] == paramToRetrieve)
return
singleParam[1];
}
}