i am developing an share point hosted online application when i am fetch Picture Library list items successfully.But the Image is not displayed in the page. Here i attached my code whats wrong? Could any one guide to me. I am a new on Share point online App.
function LoadImages() {
appWebUrl = window.location.protocol + "//" + window.location.host + _spPageContextInfo.webServerRelativeUrl;
hostWebUrl = _spPageContextInfo.siteAbsoluteUrl;
$(function () {
var utilTargetsList = "CustomerLicenceGallary";
$.ajax({
url: appWebUrl + "/_api/SP.AppContextSite(#target)/web/lists/getbytitle('" + utilTargetsList + "')/items?#target='" + hostWebUrl + "'",
method: "GET",
headers: {
"accept": "application/json;odata=verbose", "content-type": "application/json;odata=verbose", "X-RequestDigest": jQuery("#__REQUESTDIGEST").val()
},
success: function (d) {
var stringData = JSON.stringify(d);
var jsonObject = JSON.parse(stringData);
var results = jsonObject.d.results;
for (var i = 0; i < results.length; i++) {
jQuery('#Image').attr('src', results[i]["Name"]);
break;
}
},
//success: getitemssucces
error: function (error) {
console.log(JSON.stringify(error.toString()));
}
});
});
}
$(document).ready(function () {
LoadImages();
});
<div> <img id="Image" alt="Image" /></div>
PictureLibrary List
I got answer and working fine thanks all to support.
var arrayOfImageObjects = new Array();
var currentImageIndex = 0;
var hostWebUrl;
var appWebUrl;
function GetImagesAndRotate() {
appWebUrl = window.location.protocol + "//" + window.location.host
+ _spPageContextInfo.webServerRelativeUrl;
hostWebUrl = _spPageContextInfo.siteAbsoluteUrl;
// var url = appWebUrl + "/_api/SP.AppContextSite(#target)/web/lists/getbytitle('CustomerLicenceLibrary')/items?$expand=File"
//url: appWebUrl + "/_api/SP.AppContextSite(#target)/web/lists/getbytitle('" + utilTargetsList + "')/items?$top=1&#target='" + hostWebUrl + "'&$Orderby=ID desc",
jQuery.ajax({
url: appWebUrl + "/_api/SP.AppContextSite(#target)/web/lists/getbytitle('CustomerLicenceLibrary')/items?$expand=File&#target='" + hostWebUrl + "'",
type: "GET",
headers: {
"X-HTTP-Method": "MERGE",
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
},
success: function (data) {
var result = data.d.results;
for (var i = 0; i < result.length; i++) {
var image = result[i];
var imageObject = {};
imageObject.Name = image.File.Name;
imageObject.Url = image.File.ServerRelativeUrl;
imageObject.Width = image.File.ImageWidth;
imageObject.Height = image.File.ImageHeight;
arrayOfImageObjects.push(imageObject);
}
setTimeout(NextImage, 5000);
},
error: function () {
alert("request failed");
}
});
}
function NextImage() {
if (currentImageIndex < arrayOfImageObjects.length) {
jQuery('#Image').attr('src', arrayOfImageObjects[currentImageIndex].Url);
currentImageIndex++;
}
else
currentImageIndex = 0;
setTimeout(NextImage, 5000);
}
jQuery(document).ready(function () {
GetImagesAndRotate();
});
Related
I currently have the following AJAX POST request sending the FormData() of the loaded file.
It takes in however many files uploaded, places them into a new form, and then posts that form as data to the /api/file URL
$('#file').on('change', function(){
var files = $(this).get(0).files;
$('#file').ready(function(){
if (files.length > 0){
var formData = new FormData();
for (var i = 0; i < files.length; i++) {
var file = files[i];
console.log(file);
var tmppath = URL.createObjectURL(event.target.files[0]);
formData.append('userFile', tmppath);
}
$.ajax({
url: '/api/file',
type: 'POST',
data: formData,
processData: false,
contentType: false,
success: function(data){
console.log('upload successful!\n' + data);
document.getElementById('ipfs').href = 'https://ipfs.infura.io/ipfs/' + data;
document.getElementById('ipfs').innerHTML = data;
},
xhr: function() {
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener('progress', function(evt) {
if (evt.lengthComputable) {
var percentComplete = evt.loaded / evt.total;
percentComplete = parseInt(percentComplete * 100);
$('.progress-bar').text(percentComplete + '%');
$('.progress-bar').width(percentComplete + '%');
if (percentComplete === 100) {
$('.progress-bar').html('Done');
}
}
}, false);
return xhr;
}
});
}
});
and the following express node app
app.post('/api/file', async function(req,res){
console.log(await req.files);
});
And all it prints out is Null.
We are trying to give a reply to the existing topic discussion using the REST API in Microsoft SharePoint.
Below are request details.
URL:https://[TenantName].sharepoint.com/_api/web/lists/GetByTitle('dicussion board')/Items
Headers: Authorization:Bearer XXXX
Content-Type:application/json;odata=verbose
accept:application/json;odata=verbose
RequestBody:
{
"Body": "<p>Test message using api</p>",
"__metadata": {
"type": "SP.Data.dicussion_x0020_boardListItem"
},
"ParentItemID":10,
"ContentTypeId": "0x0000EE0020063889D45FD823B4A8455444117056EE3"
}
But this is creating a new topic in Sharepoint.
Can anyone help with this?
One single call can not do this based on my testing.
the script works fine.
<script type="text/javascript">
function executeJson(options) {
var headers = options.headers || {};
var method = options.method || "GET";
headers["Accept"] = "application/json;odata=verbose";
if (options.method == "POST") {
headers["X-RequestDigest"] = $("#__REQUESTDIGEST").val();
}
var ajaxOptions =
{
url: options.url,
type: method,
contentType: "application/json;odata=verbose",
headers: headers
};
if ("data" in options) {
ajaxOptions.data = JSON.stringify(options.data);
}
return $.ajax(ajaxOptions);
}
function createListItem(webUrl, listTitle, payload) {
var url = webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/items";
return executeJson({
"url": url,
"method": 'POST',
"data": payload
});
}
function moveListItem(webUrl, listTitle, itemId, folderUrl) {
var url = webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/getItemById(" + itemId + ")?$select=FileDirRef,FileRef";
return executeJson({
"url": url
})
.then(function (result) {
var fileUrl = result.d.FileRef;
var fileDirRef = result.d.FileDirRef;
var moveFileUrl = fileUrl.replace(fileDirRef, folderUrl);
var url = webUrl + "/_api/web/getfilebyserverrelativeurl('" + fileUrl + "')/moveto(newurl='" + moveFileUrl + "',flags=1)";
return executeJson({
"url": url,
"method": 'POST'
});
});
}
function getParentTopic(webUrl, listTitle, itemId) {
var url = webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/getItemById(" + itemId + ")/Folder";
return executeJson({
"url": url,
});
}
function createNewDiscussionReply(webUrl, listTitle, messagePayload) {
var topicUrl = null;
return getParentTopic(webUrl, listTitle, messagePayload.ParentItemID)
.then(function (result) {
topicUrl = result.d.ServerRelativeUrl;
return createListItem(webUrl, listTitle, messagePayload);
})
.then(function (result) {
var itemId = result.d.Id;
return moveListItem(webUrl, listTitle, itemId, topicUrl);
});
}
var listTitle = "Dicussion Board";
var webUrl = _spPageContextInfo.webAbsoluteUrl;
var messagePayload = {
'__metadata': { "type": "SP.Data.Dicussion_x0020_BoardListItem" }, //set DiscussionBoard entity type name
'Body': "Test message using api", //message Body
'FileSystemObjectType': 0, //setto 0 to make sure Mesage Item
'ContentTypeId': '0x0107001F110388B0388441BE3F004885E844F9', //set Message content type
'ParentItemID': 1 //set Discussion (topic) Id
};
function CreateReply() {
createNewDiscussionReply(webUrl, listTitle, messagePayload)
.done(function (item) {
console.log('Message(reply) has been sent');
})
.fail(function (error) {
console.log(JSON.stringify(error));
});
}
</script>
I've made some headway with the official Google API node client but I've reached a bit of a dead end figuring out how to get my video file up to youtube.
I have a video: example.mp4
I have this code:
OAuth2Client = googleapis.OAuth2Client;
var oauth2Client = new OAuth2Client('XXXXXX', 'XXXXXXX', 'http://callback_url');
googleapis.discover('youtube', 'v3').execute(function(err, client) {
if (err) console.log(err);
// I think the following is the bones of what I want to do
client.youtube.videos.insert({}).execute(function(err, client) {
if (err) console.log(err);
});
});
I only get an error using the insert method (which I expected with no params passed), the client initialises and returns fine.
I'm not sure how to pass the video (in the same directory as the script) to YouTube. Just a pointer would be greatly appreciated.
How about this code:
var googleapis = require('googleapis');
googleapis.discover('youtube', 'v3').execute(function(err, client) {
var metadata = {
snippet: { title:'title', description: 'description'},
status: { privacyStatus: 'private' }
};
client
.youtube.videos.insert({ part: 'snippet,status'}, metadata)
.withMedia('video/mp4', fs.readFileSync('user.flv'))
.withAuthClient(auth)
.execute(function(err, result) {
if (err) console.log(err);
else console.log(JSON.stringify(result, null, ' '));
});
});
You could use this package to do that:
https://github.com/h2non/youtube-video-api
This code works for me:
var file_reader = fs.createReadStream(file_path, {encoding: 'binary'});
var file_contents = '';
file_reader.on('data', function(data)
{
file_contents += data;
});
file_reader.on('end', function()
{
var xml =
'<?xml version="1.0"?>' +
'<entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:yt="http://gdata.youtube.com/schemas/2007">' +
' <media:group>' +
' <media:title type="plain">' + title + '</media:title>' +
' <media:description type="plain">' + description + '</media:description>' +
' <media:category scheme="http://gdata.youtube.com/schemas/2007/categories.cat">' + category + '</media:category>' +
' <media:keywords>' + keywords + '</media:keywords>' +
' </media:group>' +
'</entry>';
var boundary = Math.random();
var post_data = [];
var part = '';
part = "--" + boundary + "\r\nContent-Type: application/atom+xml; charset=UTF-8\r\n\r\n" + xml + "\r\n";
post_data.push(new Buffer(part, "utf8"));
part = "--" + boundary + "\r\nContent-Type: video/mp4\r\nContent-Transfer-Encoding: binary\r\n\r\n";
post_data.push(new Buffer(part, 'ascii'));
post_data.push(new Buffer(file_contents, 'binary'));
post_data.push(new Buffer("\r\n--" + boundary + "--"), 'ascii');
var post_length = 0;
for(var i = 0; i < post_data.length; i++)
{
post_length += post_data[i].length;
}
var options = {
host: 'uploads.gdata.youtube.com',
port: 80,
path: '/feeds/api/users/default/uploads?alt=json',
method: 'POST',
headers: {
'Authorization': 'GoogleLogin auth=' + auth_key,
'GData-Version': '2',
'X-GData-Key': 'key=' + exports.developer_key,
'Slug': 'example.mp4',
'Content-Type': 'multipart/related; boundary="' + boundary + '"',
'Content-Length': post_length,
'Connection': 'close'
}
}
var req = http.request(options, function(res)
{
res.setEncoding('utf8');
var response = '';
res.on('data', function(chunk)
{
response += chunk;
});
res.on('end', function()
{
console.log(response);
response = JSON.parse(response);
callback(response);
});
});
for (var i = 0; i < post_data.length; i++)
{
req.write(post_data[i]);
}
req.on('error', function(e) {
console.error(e);
});
req.end();
});
now, help me, I try to post data with ajax to nodejs server.
and server accept the postdata. and now i use http.get to request www.baidu.com, and want to get the html , at last return the html to front.but is error
front ajax
$("#subbtn").click(function(){
var keywords = $("#kw").val();
var target = $("#result");
if (!keywords){
target.html("<font color='#FF0000'>please key words</font>");
}
var keyArr = keywords.replace(/\,+/g, ",").split("\,");
for (var i = 0; i < keyArr.length; i++){
$.ajax({
type: "POST",
url: "http://127.0.0.1:10088",
data : { kw : keyArr[i]},
dataType: "json",
cache: false,
timeout: 5000,
success: function(data) {
alert(data.rank);return false;
// $("#result").append(data.num);
},
error: function(jqXHR, textStatus, errorThrown) {
alert('error ' + textStatus + " " + errorThrown);
}
});
}
});
and the server.js
// Nodejs部分,主要作用是接收前端关键词,抓取百度知道页面。返回页面给前端
var http = require('http');
var cheerio = require('cheerio');
var iconv = require('iconv-lite');
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/html', 'Access-Control-Allow-Origin' : '*' });
var postData = "";
var ret = 0;
req.setEncoding('utf-8');
req.addListener('data', function(chunk){
postData += chunk;
});
req.addListener('end', function(){
var value = postData.replace(/kw=/, ''), result = 0;
doRequest(value, 0);
});
res.end('{"rank":'+result+'}');
}).listen(10088);
/**
* GET请求Baidu
* #param kw 关键词
* #param page
*/
var doRequest = function(kw, page){
page = page * 10;
var options = {
host: 'zhidao.baidu.com',
port: 80,
path: '/search?word='+kw+'&pn='+page
};
http.get(options, function(res) {
var buffers = [], size = 0;
res.on('data', function(buffer) {
buffers.push(buffer);
size += buffer.length;
});
res.on('end', function() {
var buffer = new Buffer(size), pos = 0;
for(var i = 0, l = buffers.length; i < l; i++) {
buffers[i].copy(buffer, pos);
pos += buffers[i].length;
}
var gbk_buffer = iconv.decode(buffer,'GBK');
$ = cheerio.load(gbk_buffer.toString());
// 获取页面前三个的优质回答
var target = "DARRY RING";
var isBreak = false;
var htmlTop = $("#cluster-items").find(".con-all").slice(0, 3);
htmlTop.each(function(){
var tContent = $(this).text().replace(/\s+/g, "");
tContent = tContent.toLowerCase();
if (tContent.indexOf("darryring") > 0 ){ // 当找到DY的时候,退出循环
isBreak = true;
return false;
}
});
if (isBreak == true){
return {keyword : kw, score : 1};
}
var html = $("#wgt-list").find("dd.answer");
var n = 0;
html.each(function(i, elem){
var content = $(this).text().replace(/\s+/g, "");
content = content.toLowerCase();
if (content.indexOf("darryring") > 0 && n <= html.length ){ // 当找到DY的时候,退出循环
n = i + 1;
return false;
}
});
if(n == 0){
page++;
if (page < 5){
doRequest(kw, page);
}else{
return {keyword : kw, score : 9999};
}
}else{
var num = page + n;
return {keyword : kw, score : num};
}
});
res.on('error', function(e){
console.log("Got error: " + e.message);
})
})
}
Parsing POST data can be tricky business. I recommend using a module to help you out. Formaline has all the bells and whistles you'll need or poor-form is a lighter weight version.
I'm trying to upload video to youtube programatically. I chose to use Node.js for the task.
I get an XML response as well as an HTTP Status Code of 201 and I see the video appear in video manager, however the video always has the message "Failed (unable to convert video file)".
I can upload the file through YouTube's own uploader on their page and there are no problems. I only have to upload to a single account, so I set up the OAuth2 for the account and stored the refresh token. The refresh token is hard-coded, though I replace it with a variable below.
Does the refresh token need to, itself, be refreshed?
My code:
var qs = require('querystring'),
https = require('https'),
fs = require('fs');
var p_data = qs.stringify({
client_id: myClientID,
client_secret: myClientSecret,
refresh_token: refreshTokenForAccount,
grant_type: 'refresh_token'
});
var p_options = {
host: 'accounts.google.com',
port: '443',
method: 'POST',
path: '/o/oauth2/token',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': p_data.length,
'X-GData-Key': myDeveloperKey
}
};
var file_path = process.argv[1] || "video.mp4";
var json = "";
var p_req = https.request(p_options, function(resp){
resp.setEncoding( 'utf8' );
resp.on('data', function( chunk ){
json += chunk;
});
resp.on("end", function(){
debugger;
var access_token = JSON.parse(json).access_token;
var title="test upload1",
description="Second attempt at an API video upload",
keywords="",
category="Comedy";
var file_reader = fs.createReadStream(file_path, {encoding: 'binary'});
var file_contents = '';
file_reader.on('data', function(data)
{
file_contents += data;
});
file_reader.on('end', function()
{
var xml =
'<?xml version="1.0"?>' +
'<entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:yt="http://gdata.youtube.com/schemas/2007">' +
' <media:group>' +
' <media:title type="plain">' + title + '</media:title>' +
' <media:description type="plain">' + description + '</media:description>' +
' <media:category scheme="http://gdata.youtube.com/schemas/2007/categories.cat">' + category + '</media:category>' +
' <media:keywords>' + keywords + '</media:keywords>' +
' </media:group>' +
'</entry>';
var boundary = Math.random();
var post_data = [];
var part = '';
part = "--" + boundary + "\r\nContent-Type: application/atom+xml; charset=UTF-8\r\n\r\n" + xml + "\r\n";
post_data.push(new Buffer(part, "utf8"));
part = "--" + boundary + "\r\nContent-Type: video/mp4\r\nContent-Transfer-Encoding: binary\r\n\r\n";
post_data.push(new Buffer(part, 'utf8'));
post_data.push(new Buffer(file_contents, 'binary'));
post_data.push(new Buffer("\r\n--" + boundary + "--\r\n\r\n", 'utf8'));
var post_length = 0;
for(var i = 0; i < post_data.length; i++)
{
post_length += post_data[i].length;
}
var options = {
host: 'uploads.gdata.youtube.com',
port: 443,
path: '/feeds/api/users/default/uploads',
method: 'POST',
headers: {
'Authorization': 'Bearer ' + access_token,
'X-GData-Key': myDeveloperKey,
'Slug': 'video.mp4',
'Content-Type': 'multipart/related; boundary="' + boundary + '"',
'Content-Length': post_length,
'Connection': 'close'
}
}
var req = https.request(options, function(res)
{
res.setEncoding('utf8');
console.dir(res.statusCode);
console.dir(res.headers);
var response = '';
res.on('data', function(chunk)
{
response += chunk;
});
res.on('end', function()
{
console.log( "We got response: " );
console.log(response);
});
});
for (var i = 0; i < post_data.length; i++)
{
req.write(post_data[i]);
}
req.on('error', function(e) {
console.error(e);
});
req.end();
});
});
});
p_req.write(p_data);
p_req.end();
The problem was in the file being uploaded.
This line: var file_path = process.argv[1] || "video.mp4"; should have been var file_path = process.argv[2] || "video.mp4";
Note argv[1] is the absolute path to the script being run, argv[2] is the first command line argument passed to the script.
Of course YouTube would fail to convert the "video", it wasn't video at all it was the script being run.