- SharePoint Search REST API
- Call External REST API
- Get and Create List Items using REST API
- Get Current Login User Name Using REST API
SharePoint Search REST API
<html>
<head>
<script type="text/javascript" src="/sites/testing/SiteAssets/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
function SearchResult(clicked_id){
var searchString=document.getElementById('txtSearch').value;
var rootSite="https://<domain>/sites/testing";
$("#result").html("");
$.ajax({
dataType: 'text',
url: rootSite+"/_api/search/query?querytext='"+searchString+"'",
data: '',
success: function (data) {
xmlDoc = $.parseXML(data),
$xml = $( xmlDoc );
var appendData="";
var title="";
var link="";
var author="";
var fileType="";
var ValueType="";
$($xml.find("d\\:query>d\\:PrimaryQueryResult>d\\:RelevantResults>d\\:Table>d\\:Rows>d\\:element>d\\:Cells>d\\:element")).each(function(){
var key=$(this).find("d\\:Key").text();
if(key=="Title"){
title=$(this).find("d\\:Value").text();
}
if(key=="Author"){
author=$(this).find("d\\:Value").text();
}
if(key=="Path"){
link=$(this).find("d\\:Value").text();
}
if(key=="FileType"){
fileType=$(this).find("d\\:Value").text();
ValueType=$(this).find("d\\:ValueType").text();
}
if(key=="SPWebUrl"){
var siteUrl=$(this).find("d\\:Value").text();
if(siteUrl==rootSite)
{
if(clicked_id=="all"){
var alink =fileType+ " <a href='"+link+"'>"+title+"</a> by "+author+"</br>";
$("#result").append(alink);
}
if(clicked_id=="documents" && fileType!="aspx"){
var alink =fileType+ " <a href='"+link+"'>"+title+"</a> by "+author+"</br>";
$("#result").append(alink);
}
if(clicked_id=="list" && ValueType=="Null"){
var alink =fileType+ " <a href='"+link+"'>"+title+"</a> by "+author+"</br>";
$("#result").append(alink);
}
}
}
});
//document.getElementById('result').innerHTML=appendData;
},
error: function(){
alert("Error");
}
});
}
</script>
</head>
<body>
<input type="text" style="font-size:18px;color:gray" id="txtSearch" value="">
<div style="font-size:18px">
<a href="#" id="all" onclick="SearchResult(this.id)">All</a>
<a href="#" id="documents" onclick="SearchResult(this.id)">Document</a>
<a href="#" id="list" onclick="SearchResult(this.id)">List</a>
</div>
</br>
<div id="result" style="font-size:18px">
</div>
</body>
</html>
_____________________________________________________________________
SharePoint Call External REST API
<html>
<head>
<script type="text/javascript" src="/sites/testing/SiteAssets/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
function testws(){
$.ajax({
dataType: 'json',
url: 'https://server/emberws/requestcheck.php',
data: '',
success: function (data) {
alert(data.pets[0].name);
},
error: function(){
alert("Error");
}
});
}
function postMethod(){
var name=$('#txtName').val();
$.ajax({
type:'post',
url: 'https://server/emberws/postcheck.php',
data: {param:name},
success: function (data) {
alert(data.resp);
},
error: function(){
alert("Error");
}
});
}
$(document).ready(function (e){
$("#uploadimage").on('submit',(function(e) {
e.preventDefault();
$.ajax({
url: "https://server/emberws/uploadcheck.php",
type: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData:false,
success: function(data){
$('#rimage').attr('src', data.resp);
}
});
}));
});
</script>
</head>
<body>
<WebPartPages:SPWebPartManager runat="server"/>
<form id="form1" runat="server">
<!--GET and POST Method -->
<input name="Button1" onclick="testws();" type="button" value="Get Method" />
<br/>
<input name="Text1" id="txtName" type="text" />
<input name="Button1" onclick="postMethod()" type="button" value="Post Method" />
<br/>
</form>
<!--Upload Image -->
<form id="uploadimage" method="post" enctype="multipart/form-data">
<input type="file" name="image" id="image"/>
<input type="submit" value="Upload" class="submit" />
</form>
<img src="" id="rimage"/>
</body>
</html>
_____________________________________________________________________
SharePoint Get and Create List Items using REST API
<script type="text/javascript" src="/sites/testing/SiteAssets/jquery.min.js"></script>
<script type="text/javascript" src="/sites/testing/SiteAssets/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
function onGetCustomer(id) {
var requestUri = _spPageContextInfo.webAbsoluteUrl +
"/_api/Web/Lists/getByTitle('AccessDataList')/items("+id+")";
$.ajax({
url: requestUri,
type: "GET",
headers: { "ACCEPT": "application/json;odata=verbose" },
success: function(data){
alert(data.d.Title);
},
error: function(){
alert("Failed to get customer");
}
});
}
function CreateListItemWithDetails() {
var itemType = GetItemTypeForListName("AccessDataList");
var item = {
"__metadata": { "type": itemType },
"Name": "Test2"
};
$.ajax({
url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/getbytitle('AccessDataList')/items",
type: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify(item),
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function (data) {
alert("Success");
},
error: function (data) {
alert("Fail");
}
});
}
// Get List Item Type metadata
function GetItemTypeForListName(name) {
return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join("").slice(1) + "ListItem";
}
</script>
_____________________________________________________________________
SharePoint Get Current Login User Name Using REST API
<script type="text/javascript" src="/SiteAssets/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
var loginName = "";
var userid = _spPageContextInfo.userId;
GetCurrentUser();
function GetCurrentUser() {
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
var requestHeaders = { "accept" : "application/json;odata=verbose" };
$.ajax({
url : requestUri,
contentType : "application/json;odata=verbose",
headers : requestHeaders,
success : onSuccess,
error : onError
});
}
function onSuccess(data, request) {
var loginName = data.d.LoginName;
alert("Hello " + loginName);
// to set the "hello username" into the page
document.getElementById("id").innerHTML = "Hello " + loginName;
}
function onError(error) {
alert(error);
}
</script>
No comments:
Post a Comment