SharePoint Online SP.CamlQuery is undefined - sharepoint

I'm implementing SharePoint Online Add-in
I need SP.CamlQuery.createAllItemsQuery() data,
but SP.CamlQuery is undefined

SP.CamlQuery is defined in sp.js but your code runs when sp.js is not loaded. So to overcome this use SP.SOD.executeFunc to delay execution of your code until sp.js loads.
Note: both have SP namespace but SP.SOD.executeFunc is defined in core.js file and is always loaded in SharePoint.
Code:
SP.SOD.executeFunc('sp.js', SP.ClientContext, function() {
// do stuff, use SP.CamlQuery object
});

Related

Having Issues using Esri ArcGIS API for JavaScript in Lightning Web Component (require is not defined at eval)

I'm trying to implement Esri ArcGIS JS in Lightning Web Component. While using ArcGIS JS, the sample code uses require function to load modules. In order to do that I'm trying to use require.js. I downloaded it from here (Require.js). And then uploaded to my sandbox as static resource. I'm trying to use that static resource in my Lightning Web Component. I also added the script tag for the ArcGIS Javascript API in my Experience Cloud site's header as
<script src="https://js.arcgis.com/4.24"></script>
Lightning Web Component:
import { LightningElement, track } from 'lwc';
import { loadScript } from 'lightning/platformResourceLoader';
import requireJS from '#salesforce/resourceUrl/requireJS';
export default class TestMap extends LightningElement {
renderedCallback() {
loadScript(this, requireJS).then(() => {
console.log('requireJS loaded');
require([
"esri/geometry/Extent"
], (
Extent
) => {
var initExtent = new Extent({
xmin: -15884312,
ymin: 1634835,
xmax: -6278767,
ymax: 7505198,
spatialReference: 102100
});
});
}).catch(exception => {
console.log(exception);
});
}
}
My problem right now, eventhough I can see in the Network tab that the require.js is loaded from static resource, require function cannot be found.
Exception message catched
I'm not sure where is the issue since this is how I loaded my all javascript files before.
I was expecting to see the the require function is working after the require.js script loaded from Static Resource.
This one is a bit tricky, I will try to guide you as much as I can.
First, don't put the script tag in your website header. This is a last chance solution, we'll keep it if nothing else work.
Second, requireJS is not compatible with LWC (or Locker Service to be precise). So you can forget it. loadScript is in someways similar.
Now the solution, usually I download the whole from a CDN and host it as a static resource. Then you can load it via loadScript and use it as per the documentation.
In case the library is really small, it could be created as a LWC and then be imported but usually libraries are too heavy regarding Salesforce limit.
Looking at the library, it seems that they do not provide any compiled full version (which is probably huge). In this case I would recommend to make a custom build locally containing only the necessary pieces of code and then uploading as a static resource. Unfortunately I can't help on this part as I still didn't do it myself yet.
Feel free to comment and I will improve my answer is it's unclear.

workbox-google-analytics / service-worker offline analytics: "Cannot use 'in' operator to search for 'sync' in undefined

I have seen that other similar questions have been asked, and that the error was having the analytics code in the main js file rather than the service-worker.js file. In this case all the code is in the service-worker.js file.
Setup: node 16.0.0 (also does the same with node 15.x.x), and static site built with eleventy
For google analytics I use the analytics.js file. Basically this with a few other options added:
<!-- Google Analytics -->
<script>
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
ga('create', 'UA-XXXXX-Y', 'auto');
ga('send', 'pageview');
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>
<!-- End Google Analytics -->
The service-worker.js code in question:
import * as googleAnalytics from 'workbox-google-analytics';
googleAnalytics.initialize({
parameterOverrides: {
cd1: 'offline',
},
});
The service worker in general works absolutely fine. It has various routing routines, precache etc., and the google analytics code seems to do what it should. I can see items caching in IndexedDB when offline, and then the queue clearing when connectivity comes back. I can also see on my google analytics page hits for 'offline' as detailed in the parameterOverrides in the code above.
...so all would seem well, but in the console I still get the following:
service-worker.js?hash=4f292a2050:4259 Uncaught (in promise) TypeError: Cannot use 'in' operator to search for 'sync' in undefined
at Queue._addSyncListener (service-worker.js?hash=4f292a2050:4259)
at new Queue (service-worker.js?hash=4f292a2050:4077)
at new BackgroundSyncPlugin (service-worker.js?hash=4f292a2050:4341)
at initialize (service-worker.js?hash=4f292a2050:4515)
at service-worker.js?hash=4f292a2050:4647
at service-worker.js?hash=4f292a2050:65
I have not run the service worker through terser yet, so it is readable for anyone who wants to have a look.
https://www.thetestspecimen.com/ (then look in dev tools-->application-->service-worker.js)
I should also note that if I use the standard:
googleAnalytics.initialize();
it still does the same thing.
Obviously something is amiss, but I can't figure out what. Any suggestions appreciated
I have seen that other similar questions have been asked, and that the
error was having the analytics code in the main js file rather than
the service-worker.js file. In this case all the code is in the
service-worker.js file.
That is, in fact, what's going on here. The HTML for https://www.thetestspecimen.com/ includes
<script async="" defer="" src="/service-worker.js?hash=4afd6f55e2"></script>
which means that code which is meant to run in the context of the ServiceWorkerGlobalScope (in which self.registration is defined) is instead being run in the window global scope, where self.registration is undefined. That is what's leading to the Cannot use 'in' operator to search for 'sync' in undefined runtime error.
Just adjust your build to ensure that you don't end up with that <script> tag executing your service worker code as part of your main app, and you should be fine.

A javascript library fails to load with requirejs

Let me clear the context first. I have bought an admin panel template from themeforest and stuck on implementing that with requirejs. The seller does not provide support for implementation.
The theme requires the following JS file, in order to run:
https://altair_html.tzdthemes.com/assets/js/common.js
I have created a fiddle, where I have included the file and defined that with requirejs. But a function (moment) is not being defined. I dont know where I am doing wrong.
Check the fiddle here:
requirejs.config({
paths: {
'common': 'https://altair_html.tzdthemes.com/assets/js/common'
}
});
require(['common'], function (common) {
console.log(moment);
});
http://jsfiddle.net/tareksiddiki/q5kumwvg/
But the moment function loads when we load the external javascript (https://altair_html.tzdthemes.com/assets/js/common.js) within the <script> tag in old school way.

Issue while Instantiating SharePoint 2010 method in MSCRM 2011 plug-in

This is regarding SharePoint 2010 Integration with MSCRM 2011.
While creating a record in CRM, trying to create a Custom Document location for that record and a similar folder in sharepoint, So that when user clicks on document link in the entity record it does not prompt user to create folder in Sharpoint (Trying to avoid sharepoint noise for better user experience)
I have implemented through post create asynchronous plug-in. (I did this through console program working fine). Build the plugenter code here-in and deployed to CRM.
When creating a record it error out with a message like "An internal server 500 error - Could not load the assembly with public key token etc…blab bla bla…”
But when I am debugging the plug-in it failed at the first line of command where I am instantiating sharePoint method Create client context of sharepoint, it says [System.Security.SecurityException]={“That assembly does not allow partially trusted callers”.}
As per google, per this issue it should be having one attribute “Allow partial users” in assembly info file. As per my understanding, this should be done in because the request goes from CRM plug-in to SharePoint dll. I mean share point dlls are not allowing request from my assembly. How can we change that?
I have referenced Microsoft.SharePoint.client.dll and Microsoft.SharePoint.Client.Runtime.dll
What is the alternate to overcome this issue?
Appreciate if some one can help me ..Thanks In advance.
Here is my code for SharePoint
ClientContext clientContext = new ClientContext(siteUrl)
CredentialCache cc = new CredentialCache();
Cc.Add(new Uri(siteUrl), "NTLM", CredentialCache.DefaultNetworkCredentials);
clientContext.Credentials = cc;
clientContext.AuthenticationMode = ClientAuthenticationMode.Default;
Web web = clientContext.Web;
SP.List list = web.Lists.GetByTitle(listName);
ListItemCreationInformation newItem = new ListItemCreationInformation();
newItem.UnderlyingObjectType = FileSystemObjectType.Folder;
newItem.FolderUrl = siteUrl + "/" + folderlogicalName;
if (!relativePath.Equals(string.Empty))
newItem.FolderUrl += "/" + relativePath;
newItem.LeafName = newfolderName;
SP.ListItem item = list.AddItem(newItem);
item.Update();
clientContext.ExecuteQuery();
Where I am passing the siteurl, folderlogicalname,relativepath and new foldername as parameters.
This works fine from my Console application. But when converted to CRM plug-in it gives the above specified issue
I've seen a similar issue before.
CRM plugins run inside a sandbox, so all assemblies and .NET libraries used must allow partial trust callers (since the CRM sandbox runs under partial trust). It works in the console because you are executing the code as a full trust user in that context.
This issue is not necessarily your code, but could be a dependency or a .NET library itself does not allow partial trust callers - in your case it sounds like the Sharepoint library is the culprit (but a stack trace of the error should reveal exactly where the cause is).
Since you don't have access to the source library causing the problem, to overcome the error you will likely have to create a wrapper. However, the problem is the wrapper cannot directly reference the problem library or you will get the same issue. So to get around this, you may have to create a web service which acts as your wrapper and then call the web service in your CRM plugin. This way the full trust code is executed by the web service (which is full trust) and then returns the result to your calling CRM plugin.
Here is more info on the error.
Thanks Jason. This works for me.
I Would like to add additional few points to the answer.
1. I have added the sharepoint dlls to the bin folder of CRM 2011 site.
2. Also deployed the same dlls in the folder whereever Async job is running to make my Async plug-in to work.
Thanks once again for the cooperation

Calling Office Communicator via Silverlight Out of Browser

I need to invoke office communicator to create a chat window and phone call directly from Silverlight when running out of browser. When running in browser I do this and it works pretty well:
System.Windows.Browser.HtmlPage.Window.Eval(String.Format("window.open(\"sip:{0}\", target=\"_self\");", sip));
When running out of browser as far as I have gotten is to invoke the Communicator.UIAutomation via a dynamic but honestly I don't know what to do next.
dynamic communicator = AutomationFactory.CreateObject("Communicator.UIAutomation");
Anyone have any suggestions on how to make this work? Searching has yeilded zero results.
A couple thoughts:
Have you tried making the automated Communicator object a var, then setting a breakpoint and digging into the resulting hydrated object? You might find some methods or properties on the object you can use to make things happen.
There's a blog here that describes the Office Communicator SDK and has some sample projects. I think you might be able to include the SDK assemblies in your OOB app and automate Communicator using Microsoft's provided SDK.
The SDK has to be preinstalled in the user machines. There's no easy way to deploy it along your Silvelright OOB application.
You will need the SDK.
You can check the documentation for more details here: C:\Program Files (x86)\Microsoft Office Communicator\SDK\OCSDK.chm
It mainly refers to C#, but most of it could easily be ported to Com Automation. As an example look at the following code to start a conversation
dynamic comm = new ActiveXObject("Communicator.UIAutomation");
dynamic msgrAdv = comm.IMessengerAdvanced;
if(msgrAdv!=null)
{
try
{
object obj = msgrAdv.StartConversation(
1, //CONVERSATION_TYPE.CONVERSATION_TYPE_IM,
sipUris, // object array of signin names
null,
"Testing",
"1",
null);
windowHandle = long.Parse(obj.ToString());
}
catch (COMException ex)
{
this.writeToTextBox(
formReturnErrors.returnComError(ex.ErrorCode)
);
}
I hope this help. Noticed that from the example in the help file I changed some of the members that are defined in the .NET Assembly (which can't be referenced from your C# code). If you need this, I would suggest opening the CommunicatorAPI.dll assembly in Reflector.

Resources