1

I have a CEWP which references to

/SiteAssets/test.html

this works well. Now, I would like to pass some parameters like

/SiteAssets/test.html?category=abc

I receive these parameters with a JS script in test.html.

Does anybody have a solution to pass parameters to a html which is included in a CEWP. I do not want to add the parameters to the url in the browser, just in the CEWP.

We use SP2013 on-prem.

3
  • You mean when you reopen the properties pane the URL parameters are stripped from the URL? Mar 28 '17 at 8:31
  • No. I don't have any parameters in the URL (so not like /SitePages/Homepage.aspx?parameter1=value1&...). I just insert the parameter in the URL field of the CEWP. I just noticed, it doesn't work in edit mode either (had a caching problem, sorry). I edited my question.
    – Södy
    Mar 28 '17 at 8:36
  • Ah, ok. Yes, it looks like it. But when I edit the CEWP again, the URL parameters are still there.
    – Södy
    Mar 28 '17 at 11:49
2

With a CEWP SP Injects all your (HTML/JS) content server side into the ASPX page

So the client doesn't know about your source files,
neither is its URL included in any attributes

Any URL parameters you include are ignored (since the Server does not proces them either)

If you switch to JSLinks or UserCustomActions, those JS files will be loaded in the HEAD section with the full URI
So if you know how to get that SCRIPT DOM element you can extract the URL parameters


Passing SharePoint Data Harry Potter style

There is some [unwanted?] SharePoint behaviour that might help or not

You can pass data to your code by setting the TITLE field (or any extra Field you added to the (Site Assets) Library.

This works because SharePoint F*ucks Up HTML documents that do not have a HEAD section... remember, by origin SharePoint is a Content Management System, which controls any WebPublishing related documents

Create a Site Assets Script file: sp_testhtmlinjection.html

<H1 id="myTitle">Hello World!</H1> <SCRIPT> window.setTimeout(function(){ var myTitle = document.getElementById('myTitle'); try { myTitle.innerHTML = document.querySelectorAll("title")[1].innerHTML } catch (e) { myTitle.innerHTML += ' No Parameters!'; } },10); </SCRIPT>

Add a CEWP and link it to this file

Since there can only be one TITLE in a Document, querySelectorAll("title")[1] causes an error

The outcome as expected is:

Now go back to the Assets library and change the Title

๐ŸŒŸ ๐ŸŒŸ๐ŸŒŸ ๐ŸŒŸ๐ŸŒŸ ๐ŸŒŸ๐ŸŒŸ

As HTML files are under publishing control SharePoint will write its Metadata to the file, and adds the (SP thinks it is missing) HEAD section:

Reload the CEWP

So you can now get at that second TITLE (which does not do anything to the document since only the TITLE in the real HEAD is processed by the Browser)

Since the HTML is after the SCRIPT tag you have to wait some milliseconds

Voldemort!

Remember, when you upload a new HTML version, the metadata is in SharePoint, not in the file. The moment any metadata on the file changes SharePoint will write all metadata to the file... just try adding new Fields on the Library.

You can use the standard JavaScript TreeWalker API to get those properties from HTML comments.

This works for any Document Library, not tied to Site Assets.

Think what you can do with workflows...

This can be very powerful,

but it also really sucks when you start to work with W3C standard WebComponents, as every HTML file is rewritten by SharePoint (the moment SharePoint updates the file metadata)

Microsoft knows about it:

https://github.com/SharePoint/sp-dev-fx-webparts/issues/153

iJS iSX

Your Answer

By clicking โ€œPost Your Answerโ€, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.