Office Addin _spPageContextInfo Undefined - sharepoint

I am getting _spPageContextInfo Undefined on office Addins
i added the below sharepoint js files into my project but still getting the same error _spPageContextInfo Undefined,
<script src="Scripts/MicrosoftAjax.js"></script>
<script src="Scripts/init.js"></script>
<script src="Scripts/sp.core.js"></script>
<script src="Scripts/sp.runtime.js"></script>
<script src="Scripts/sp.js"></script>
$(document).ready(function () {
ExecuteOrDelayUntilScriptLoaded(runCode, "sp.js");
});
function runCode() {
var userid = _spPageContextInfo.userId;
}
Most examples use sharepoint hosted addin but i would like to used Office Addin.
Is there any js file i should add ? I couldn't use the guide because it only work for documents created with the addin content type https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/create-a-sharepoint-add-in-that-contains-a-document-template-and-a-task-pane-add

The _spPageContextInfo is only injected by Sharepoint when you are on a sharepoint classic page. Even for Sharepoint framework components they don't recommend to use it because it might not be on the page.

Related

OneDrive SDK : how can I use single tenant app to access onedrive files?

AADSTS50194: Application 'censured'(-app) is not configured as a multi-tenant application. Usage of the /common endpoint is not supported for such applications created after '10/15/2018'. Use a tenant-specific endpoint or configure the application to be multi-tenant.
How can I use single-tenant specific endpoint? I want to create a file picker that permit user to browse its file and organization files. I already read this question but, without editing OneDrive.js (library), I can't change
https://login.microsoftonline.com/common
to
https://login.microsoftonline.com/MY_TENANT_NAME
Hope you can help me, thanks.
because the onedrive.js library has the common endpoint hard coded, your easiest way is to change it to point to your tenant login endpoint.
if you don't want to do that, have the user login before using the picker, then save the access token and endpoint and provide it to the js
similar to whats being done here: https://github.com/OneDrive/onedrive-explorer-js/blob/master/index.html
as per https://learn.microsoft.com/en-us/onedrive/developer/controls/file-pickers/js-v72/open-file?view=odsp-graph-online#advanced-options the advanced options of the picker gives you options to specify the endpointhint and accesstoken.
hope that helps,
Update
I just tried it this way and it seems to work. but I didn't try to do it with a token. just an endpointHint, when I used an endpointHint, it didn't give me the error about the multi-tenant issue.
<html>
<head>
<script type="text/javascript" src="https://js.live.net/v7.2/OneDrive.js"></script>
<script type="text/javascript">
function launchOneDrivePicker(){
var odOptions = {
clientId: "myappid-guid-thing",
action: "query",
multiSelect: true,
advanced: {endpointHint: "https://azuretenant-my.sharepoint.com/",},
};
OneDrive.open(odOptions);
}
</script>
</head>
<body>
<button onClick="launchOneDrivePicker()">Open from OneDrive</button>
</body>
</html>
Please make sure you get the endpoint url right, eg, https://tenantname-my.sharepoint.com notice the "-my" after your tenant name, that's necessary.

Accessing logged in user roles and include in page source

We are intergrating an external JavaScript application into Acumatica and we have a need to be able to access the logged in users authorization / user access roles. Our thought is that if we can write the logged in users access roles to the page source as global scope variables our JavaScript app can handle the rest, but we are a bit challenged in figuring out how to do that. We know we can write the roles to the trace screen, but that doesn't help as we need it literally in the page source for this to work (ideally the page source and not the DOM - but we can look into if the DOM could work too).
Any help would be much appreciated.
The page source in ASP.Net is a ASPX.CS file that resides on the server, I doubt your JavaScript can hook into that. The communication from the server to the UI layer is a template engine.
You define the fields in the ASPX file and these template fields are populated with the DataViews current record. The minified JavaScript that is in the page runs that templating engine.
You can't simply generate dynamic HTML or access the JavaScript side of things from the server. So having fields values in the DOM is probably your best bet. You can make the controls invisible if required.
Example to read UI control values using JavaScript:
<%# MasterType VirtualPath="~/MasterPages/FormDetail.master" %>
<asp:content id="cont1" contentplaceholderid="phDS" runat="Server">
<script language="javascript" type="text/javascript">
window.addEventListener('load', function () {
// Fetch a value from UI control
var control = px_alls["edControlID"];
var value = control.getValue();
});
</script>
[...]
</asp:content>

SharePoint provider-hosted app - get link to remote web

I'm currently evaluating development approaches for provider-hosted apps for SharePoint Online.
I already found the Office PnP samples, which are a quite good start.
But there is one question I could not find a solution so far.
I want to place a simple link in my host web to my app in the remote app (javascript override).
How can I get the remoteurl?
I assume that it is stored somewhere because I have to enter it during app registration.
Thanks in advance,
Florian
So here is the approach:
1. Add an aspx page in App project(not in remote web).
2. In aspx page add
<WebPartPages:AllowFraming ID="AllowFraming1" runat="server" />
<head>
<title></title>
...Add Head content
</head>
<body>
<a href='#' target="_parent" id="btnABC">
Your Link</a>
<script type="text/javascript">
var params = document.URL.split("?")[1].split("&");
var stdTokens = document.URL.split("?")[1];
var appRURL;
for (var i = 0; i < params.length; i = i + 1) {
var param = params[i].split("=")
if (param[0] == "appRURL") {
appRURL = decodeURIComponent(param[1]);
}
}
document.getElementById("btnABC").href = appRURL + "/Home/Index/?" + stdTokens;
</script>
</body>
Now, Add the client web part in same project and in source choose just created aspx page which might look like "~appWebUrl/Pages/Your.aspx"and content tag looks like
You are ready. Install the app in your host web and add this web part where you want to add Remote Web link.

how do you open another website from my website

hi can anyone help me please
what i am trying to do is get another website to open in a new window / tab
so someone would go to my site and not only would my website open but also the other website in a new / tab window without having to click any link or any thing it would just automatically open the other site in new tab/ window
This is for opening another website in a new window/tab once your website has loaded completely.You don't need to click anything.Once your website is completely loaded,other website will automatically open.I have use www.thesiteIwant.com as the URL for the other site you want to open.
window.open("http://www.thesiteIwant.com");
The above code by default opens a new window but if the user has selected to open new URLs in new tab then it will open the above site in new tab.
Complete code:
<html>
<head>
<script>
function opennewsite()
//can be placed inside the head tag
{
window.open("http://thesiteIwant.com");
}
</script>
<title> mysite </title>
</head>
<body onload="opennewsite()">
/*adding onload event handler will execute the function once your site body (content) has been completely loaded*/
//all the content here.
</body>
</html>
you can try like this
<script type="text/javascript">
window.open('www.thedomain you want.com','_blank');
</script>
although based on the browser and it's settings it might work ok or not...

How to change the default InfoPath form server Url for a form library in SharePoint

By default MOSS directs browser enabled InfoPath forms to the /_layouts/formserver.aspx page, with the query string parameters that define the form to display or edit. We have defined our own page (for a number of reasons) and would like to direct the forms in the form library there. The page can be hosted in that same _layouts folder, in a document library, it doesn't really matter.
Seems like there should be a linkeage (hopefully in a form of a configuration setting) that tells SharePoint where to direct the forms.
Thanks!
If you are using the SharePoint lists or form libraries to display the list of forms, you could add a jquery rewrite (in either a Custom Editor Web Part or Master Page) to look for links to the FormServer.aspx link.
<script type="text/javascript" src="~/_layouts/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
//Rewrite Form Links to Open in Custom Page
$("a[href*='/_layouts/FormServer.aspx']").each(function()
{
var formFileName = this.href.figureOutWhatFormAndParamsYouNeed()
var formServerUrl = 'https://server.example.com/_layouts/CustomPage.aspx'
this.href = formServerUrl
});
})
</script>
Have a look at the ServerFiles in the 12 Hive under 'Template\XML'. This has file extensions and a mapping to a redirect URL.
You can see there is a mapping for XSN and XML files in relation to InfoPath.
I haven't tried this and obviousily the normal caveats apply when altering files in the 12 Hive.

Resources