Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Get Host Web List data in SharePoint-Hosted App
WhatsApp
Chayani Sarkar
9y
11.8
k
0
0
25
Blog
Please follow the below steps:
Steps:
Create a list in the main site (Host web) as below:
Open Visual Studio à Create a new SharePoint App Project.
Select the Host for the app as “SharePoint-Hosted”.
Once the project is created, you will find the below structure.
Add the below code to the App.js file.
var appWebContext;
var listResult;
var hostweburl;
$(document).ready(function () {
getListData();
});
function getListData() {
appWebContext =
new
SP.ClientContext.get_current();
hostweburl = decodeURIComponent($.getUrlVar(
"SPHostUrl"
));
var hostwebContext =
new
SP.AppContextSite(appWebContext, hostweburl);
var list = hostwebContext.get_web().get_lists().getByTitle(
'TestList'
);
alert(list);
var query =
new
SP.CamlQuery();
query.set_viewXml(
"<View><Query><Where><Contains><FieldRef Name='Title' /><Value Type='Text'>Test</Value></Contains></Where></Query></View>"
);
listResult = list.getItems(query);
appWebContext.load(listResult);
appWebContext.executeQueryAsync(onSucceded, onFailed);
}
function onSucceded(sender, args) {
var enumerator = listResult.getEnumerator();
while
(enumerator.moveNext()) {
$(
'#message'
).append(
" "
+ enumerator.get_current().get_item(
"Name"
));
}
}
function onFailed(sender, args) {
alert(
'Failed to get host web: '
+ args.get_errorDetails());
}
// jQuery plugin for fetching querystring parameters
jQuery.extend({
getUrlVars: function () {
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf(
'?'
) + 1).split(
'&'
);
for
(var i = 0; i < hashes.length; i++) {
hash = hashes[i].split(
'='
);
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return
vars;
},
getUrlVar: function (name) {
return
jQuery.getUrlVars()[name];
}
});
Go to AppManifest.xml
à
Permissions tab. We need to give permission to the app to access the host web. Select “SiteCollection”
Permission Level ”Manage” (I have given Manage, since I am planning to edit the data as well, which I will do later). Read permission can also be given.
Deploy the solution, Trust
the app.
You will the data from the “Name” column of the host web list “TestList” on the App page. [
The formatting of the data shown in App default page is not done presently
].
Get Host Web List data in SharePoint-Hosted App
Up Next
Difference Between Hosted, Provider Hosted and SharePoint Hosted App
Ebook Download
View all
SharePoint Framework (SPFx) A Developers Guide
Read by 11.1k people
Download Now!
Learn
View all
Membership not found