Jump to content

Automatic Javascript script updating?


barchiel33

Recommended Posts

I was wondering if anyone has come up with a way to automatically update a Javascript script across multiple reports from a single source? So, for example, if the "current" version of a shared Javascript script was stored as a .js or .txt file on Sharepoint, is there a way to pull that file into a report and replace the existing instance of the script the file contains? I can see how to do the "replace existing instance" part, but I'm stumped on the file pull part. I think I could figure out how to do it using a mapped network drive, but that's not a viable solution since the script is meant to function on multiple different users accounts.

Link to comment
Share on other sites

Hello @Olivier Keugue Tadaa!

1. I do know how to achieve this with a txt file (haven't tried with a js file) from a local folder, since that doesn't require authorization. However, I'm not sure if the way I'm doing it is super efficient, so I would be open to suggestions on ways to do that as well. Yes, I'm trying to accomplish it via Sharepoint.

2. I was using Ironpython but I haven't used the Application Profiler (as far as I'm aware, that tool isn't enabled by the company I work for, but I could be wrong. I don't have administrator authority.)

Link to comment
Share on other sites

Hello @barchiel33 Are you using SharePoint on premise or cloud?  The later might require an additional authentic step. In any case, if you are able to access the file from OneDrive or some sort of network drive with IronPython to pull the script from there, then you  can use the Spotfire Script Management API to create or edit scripts and register them in text areas.

Link to comment
Share on other sites

Hello @Jose Leviaguirre!

The company I work for uses SharePoint online (ie. cloud I believe). I was trying to avoid using a network drive since that seems to require mapping the drive (possible to automate but a bit of a headache). I hadn't considered OneDrive, I'm not sure how I would get a file from there into Spotfire other than by syncing locally and loading (which wouldn't work how I want, since it'd split the file into rows).

Link to comment
Share on other sites

Hello @barchiel33 A possible solution could be to use the SharePoint API to fetch the file and then leverage the Spotfire Script Management API through IronPython. Since IronPython is a .NET implementation of Python, you can import the necessary .NET classes to interact with SharePoint. If a process works in .NET, you can replicate it in IronPython to retrieve the script content and then push updates across reports. This approach could streamline the process without needing mapped drives, while giving you centralized control over script updates. Let us know if you need more help or if you find a better approach.

Unfortunately I do not have access to SharePoint at the moment, but I believe this could work with some tweaks:

IronPython

import clr
clr.AddReference("System.Net.Http")
clr.AddReference("System.Json")

from System.Net.Http import HttpClient, HttpRequestMessage, HttpMethod, HttpContent
from System.Json import JsonValue
import System

# Variables
sharepoint_site = "https://yourcompany.sharepoint.com/sites/yoursite"
file_path = "/Shared Documents/yourfile.js"  # Path to the file in SharePoint
access_token = "your_access_token"  # Retrieve OAuth token for SharePoint API

# Function to get the JavaScript content from SharePoint
def fetch_js_from_sharepoint(site_url, file_path, token):
    client = HttpClient()
    request = HttpRequestMessage(HttpMethod.Get, f"{site_url}/_api/web/GetFileByServerRelativeUrl('{file_path}')/$value")
    
    # Add Authorization Header with Bearer token
    request.Headers.Add("Authorization", f"Bearer {token}")

    response = client.SendAsync(request).Result

    if response.IsSuccessStatusCode:
        js_content = response.Content.ReadAsStringAsync().Result
        return js_content
    else:
        print("Failed to fetch the file:", response.StatusCode)
        return None

# Fetch JavaScript content
js_content = fetch_js_from_sharepoint(sharepoint_site, file_path, access_token)

# Use the content in Spotfire Script Management API (replace the existing script)
if js_content:
    from Spotfire.Dxp.Application.Scripting import ScriptDefinition
    # Locate the existing script in Spotfire by name or ID
    script_service = Application.GetService(ScriptDefinition)
    existing_script = script_service.GetScriptByName("YourScriptName")

    # Replace script content
    if existing_script:
        existing_script.Script = js_content
        print("Script updated successfully.")
    else:
        print("Script not found.")
else:
    print("JavaScript content could not be retrieved.")

 

 

Edited by Jose Leviaguirre
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...