Hallo,
i would like to do something that i thought would be fairly simple:
get the loginName of the current user using the SharePoint2010 Client OM with ECMAScript.
Here is my code:
var currentcontext = new SP.ClientContext.get_current();
var currentweb = currentcontext.get_web();
currentcontext.load(currentweb);
var currentuser = currentweb.get_currentUser();
currentcontext.load(currentuser);
var loginName = currentuser.get_loginName();
the last line throws an exception:
"The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested."
But why? I've loaded the 'currentuser', so why is the 'property or field' not initialized?
Here is a more complete example with executeQueryAsync:
SP.SOD.executeOrDelayUntilScriptLoaded(runMyCode, "SP.js");
function runMyCode() {
var ctx = new SP.ClientContext.get_current();
var web = ctx.get_web();
ctx.load(web);
var user = web.get_currentUser();
user.retrieve();
ctx.executeQueryAsync(
function () {
//only in the success case you can work with user login
doSomethingWithUserLogin(user.get_loginName());
},
function (data) {
//notify the failure
});
}
You need to retrieve the user first and load the web, not the user.
The following should work:
var currentcontext = new SP.ClientContext.get_current();
var currentweb = currentcontext.get_web();
currentcontext.load(currentweb);
var currentuser = currentweb.get_currentUser();
currentuser.retrieve();
currentcontext.load(currentweb);
var loginName = currentuser.get_loginName();
For an even better example using an asynchronous query check the following tutorial:
SharePoint 2010 ECMAScript - How to know logged in user information
Related
The following is the code I use to get text out of slack and write it into a google sheet.
Actually everything works perfect except the fact that the "Success" message is only seen by the person that uses the command and I couldnt find any way to make it visible to all user in the channel.
So that is also my question: How do I change this? How do I make the "Success" message visible to all users in the channel?
function doPost(e) {
if (typeof e !== 'undefined') {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Doorbug');
var lastRow = sheet.getLastRow();
//relevant data
var parameter = e.parameter;
var text = parameter.text;
var userName = parameter.user_name;
var channel = parameter.channel_name;
var date = new Date();
//reg-number, Email, location, UA
var data = text.split(',');
var reg = data[0];
var email = data[1];
var location = data[2];
var ua = data[3];
var slackDetails = [date, reg, email, location, ua, userName, channel];
//paste the data in the sheet
sheet.getRange(lastRow + 1,1,1,7).setValues([slackDetails]);
}
//return message for the user
return ContentService.createTextOutput('Success :)');
}
I'm guessing this Apps Script project is published as a web application and this function is being used by a Slack slash command. Please update the question providing more context if this assumption isn't correct.
If so, you want to follow their instructions regarding message visibility when sending a response.
In particular, instead of sending a simple plain text response, you want to respond with a JSON-encoded message like this:
var response = "Success :)";
return ContentService.createTextOutput(JSON.stringify({
response_type: "in_channel",
text: response,
}));
Furthermore, you may want to reconsider the logic for crafting your response. As it stands, your function will always respond with "Success :)", even if the initial error checking evaluates to false and the function ends not inserting data into the sheet.
(Noobish at JS) I'm setting up a Discord Bot command that allows it so the user types out a embedded message. I'd like to use arguments and use it in a command like this
d!cembed ~title~~description~~footer~~color~~thumbnail url~
Code I'm using
async run(message, args)
{
var titleargs = message.client
var descriptionargs = message.client
var footerargs = message.client
var urlargs = message.client
{
{
var myInfo = new discord.RichEmbed()
.setTitle(titleargs)
.setDescription(descriptionargs)
.setFooter(footerargs)
.setColor(0xff0000)
.setThumbnail(urlargs)
message.channel.send(myInfo);
}
}
}
}
Result Expected : The author of the message / command will create a embedded message
Actual Result : A error appears in terminal
Alright, I'm sorry if this is not a great answer but, I will do my best.
var titleargs = message.client
var descriptionargs = message.client
var footerargs = message.client
var urlargs = message.client
message.client would not work in this sense. Client and message are both classes, you would want to use:
var titleargs = message.content;
var descriptionargs = message.content;
var footerargs = message.content;
var urlargs = message.content;
Content is a property of the message class which allows you to use user input.
https://discord.js.org/#/docs/main/stable/class/Message?scrollTo=content
Furthermore,
var myInfo = new discord.RichEmbed()
.setTitle(titleargs)
.setDescription(descriptionargs)
.setFooter(footerargs)
.setColor(0xff0000)
.setThumbnail(urlargs);
message.channel.send(myInfo);
.setThumbnail(url) will only work with a correctly formatted url.
You can find more on that here, https://discord.js.org/#/docs/main/stable/class/RichEmbed?scrollTo=setThumbnail as well as the other methods for RichEmbed. I hope this helps!
I am trying to develop a friend request system for my website using cloud functions to send, receive, and accept the requests. Yet I am not able to call them. I scoured through all the docs and modified my code to meet the right protocol but still no cigar. Pretty lost at this point, any help would really be appreciated.
Heres my cloud function
exports.sendFriendRequest = functions.https.onCall((data, context) => {
var jsonData = JSON.parse(data);
var requestedUserProfileRef = firebase.database().ref("Users/" + jsonData["recievingUser"]);
requestedUserProfileRef.child("FriendRequests").push();
var pushKey = requestedUserProfileRef.key;
requestedUserProfileRef.set(jsonData["sendingUser"]);
console.log(requestedUserProfileRef.Name);
});
Heres how I am calling it, or trying to at least
function sendFriendRequest(userUid)
{
//userUid is user that will recieve request
var curUser = firebase.auth().currentUser;
userUid = userUid.substring(1);
var sendRequest = firebase.functions().httpsCallable('sendFriendRequest');
sendRequest({"data": {"sendingUser": curUser.uid, "recievingUser": userUid}}).then(function(result) {});
}
Could it have something to do with not having a result returned?
Finally, here is the error that I am getting when I try and call the function
POST https://us-central1-accounts-cfe00.cloudfunctions.net/sendFriendRequest
Uncaught (in promise) Error: INTERNAL
at new t (firebase.js:1)
at _errorForResponse (firebase.js:1)
at e.<anonymous> (firebase.js:1)
at firebase.js:1
at Object.next (firebase.js:1)
at a (firebase.js:1)
Yes it's happening because you returned nothing. Return the promise like this:
exports.sendFriendRequest = functions.https.onCall((data, context) => {
var jsonData = JSON.parse(data);
var requestedUserProfileRef = firebase.database().ref("Users/" +
jsonData["recievingUser"]);
var promise = requestedUserProfileRef.child("FriendRequests").push();
var pushKey = requestedUserProfileRef.key;
return requestedUserProfileRef.set(jsonData["sendingUser"])
.then(function(result) {
console.log(requestedUserProfileRef.Name);
};
});
I have this code
var englishSubtitle = new chrome.cast.media.Track(2,chrome.cast.media.TrackType.TEXT);
englishSubtitle.trackContentId = 'english.vtt';
englishSubtitle.trackContentType = 'text/vtt';
englishSubtitle.subtype = chrome.cast.media.TextTrackType.CAPTIONS;
englishSubtitle.name = 'English';
englishSubtitle.language = 'en-US';
englishSubtitle.customData = null;
var tracks = englishSubtitle;
var mediaInfo = new chrome.cast.media.MediaInfo(app.streamState_.manifest);
mediaInfo.contentType = app.streamState_.type;
mediaInfo.metadata = new chrome.cast.media.GenericMediaMetadata();
mediaInfo.customData = null;
mediaInfo.streamType = chrome.cast.media.StreamType.BUFFERED;
mediaInfo.textTrackStyle = new chrome.cast.media.TextTrackStyle();
mediaInfo.tracks = tracks;
mediaInfo.metadata.metadataType = chrome.cast.media.MetadataType.GENERIC;
var activeTrackIds = [2];
var request = new chrome.cast.media.LoadRequest(mediaInfo);
request.autoplay = true;
request.currentTime = 0;
request.activeTrackIds = activeTrackIds;
session.loadMedia(request,onMediaDiscovered.bind( this, 'loadedMedia'), onMediaError);
I want to show subtitle on chromecast. When I want to set activeTracks on the request, I receive an error
Object {code: "session_error", description: "INVALID_PARAMS", details: Object}
The subtitle it doesn't show and the video doesn't play it at all, because of that error.
Am I doing something wrong?
tracks should be an array when you set
mediaInfo.tracks = tracks;
In your case, you should try
var tracks = [englishSubtitle];
and as was said earlier, use SUBTITLES instead of CAPTIONS. Finally make sure you have CORS headers present from your web server even if you are using mp4.
tracks should be stored inside an array
https://developers.google.com/cast/docs/reference/chrome/chrome.cast.media.MediaInfo#tracks
Array of non-null chrome.cast.media.Track
Array of Track objects.
mediaInfo.tracks = [englishSubtitle, frenchSubtitle, germanSubtitle]
I've created a simple javascript wrapper for the chromecast SDK:
https://github.com/Fenny/ChromecastJS
Might be worth to check out if you stumble upon more problems, good luck!
I'm using localStorage in my Firefox add-on in this way:
var ioService = Components.classes['#mozilla.org/network/io-service;1'].getService(Components.interfaces.nsIIOService);
var scriptSecManager = Components.classes['#mozilla.org/scriptsecuritymanager;1'].getService(Components.interfaces.nsIScriptSecurityManager);
var domStorageManager = Components.classes['#mozilla.org/dom/storagemanager;1'].getService(Components.interfaces.nsIDOMStorageManager);
var uri = ioService.newURI('http://example.com/addon/', null, null);
var principal = scriptSecManager.getNoAppCodebasePrincipal ? scriptSecManager.getNoAppCodebasePrincipal(uri) : scriptSecManager.getCodebasePrincipal(uri);
var localStorage = domStorageManager.getLocalStorageForPrincipal(principal, '');
All works fine, but when user removes "offline data" storage is cleared. How can i workround it?
Use the preferences service to store the data in the user's profile.
var prefService = Cc["#mozilla.org/preferences-service;1"].getService(Ci.nsIPrefService),
prefs = prefService.getBranch("extensions.YOUREXTENSIONABBREVIATIONHERE.");
var stringValue = "My Name",
booleanValue = true,
numberValue = 55;
prefs.setCharPref("stringParam", stringValue);
prefs.setBoolPref("booleanParam", booleanValue);
prefs.setCharPref("numberParam", ''+numberValue);
prefService.savePrefFile(null); // only to force an immediate save
stringValue = prefs.getCharPref("stringParam");
booleanValue = prefs.getBoolPref("booleanParam");
numberValue = parseInt(prefs.getCharPref("numberParam"), 10);
Put a defaults.js file in your defaults/preferences folder and initialize preferences for a new user. You'll get an error if you try to retrieve a preference that doesn't exist and there's no way to check if it exists.
Look here:
https://developer.mozilla.org/en-US/docs/Adding_preferences_to_an_extension