Connect to SharePoint through a Custom Widget

2281
4
Jump to solution
11-01-2016 04:40 AM
ZdeněkSoldán
Occasional Contributor

Hello,

I need to send a file from my widget to SharePoint site on button click. First I tried to connect to SHP via SHP REST API. In my function I have this code get from Complete basic operations using SharePoint 2013 REST endpoints :

var appweburl = "https://server.domain.cz/";
        var executor = new SP.RequestExecutor(appweburl);
        executor.executeAsync(
           {
              url:
                 appweburl +
                 "/_api/SP.AppContextSite(@target)/web/lists?@target='" +
                 hostweburl + "'",
              method: "GET",
              success: successHandler,
              error: errorHandler
            }
           );‍‍‍‍‍‍‍‍‍‍‍‍‍

I need to define a SP.js file in a header if my widget code but I don't know how. This file should be a part of SharePoint installation folder I guess. But how can I referenced it in my widget?
Thanks for any help.

Zdenek Soldan

0 Kudos
1 Solution

Accepted Solutions
ZdeněkSoldán
Occasional Contributor

Thank you for quick response Robert.

I found what I looking for here: javascript - Using SharePoint 2013 Cross-Domain Library in a simple web page: is it possible? - Stac...  

The file SP.js can be load through jQuery from hostweburl/_layouts/15/SP.RequestExecutor.js 

View solution in original post

4 Replies
RobertScheitlin__GISP
MVP Emeritus

Zdeneck,

   If you look at the help doc it states:

  • Put the library in the widget or theme folder and add it to your widget or theme dependency array. Use this approach to load an AMD module that only applies for one widget or theme.

Use other libraries—Web AppBuilder for ArcGIS (Developer Edition) | ArcGIS for Developers 

ZdeněkSoldán
Occasional Contributor

Thank you for quick response Robert.

I found what I looking for here: javascript - Using SharePoint 2013 Cross-Domain Library in a simple web page: is it possible? - Stac...  

The file SP.js can be load through jQuery from hostweburl/_layouts/15/SP.RequestExecutor.js 

RobDunfey
Occasional Contributor

Hi Zdenek,

I had a look at the Stack Exchange post you mention, but couldn't figure it out.  Would you be able to share some sample code?  Or are you aware of any other SharePoint custom widgets?

Kind Regards,

Rob

0 Kudos
ZdeněkSoldán
Occasional Contributor

Hello Rob,

Sure I can. There are a lot of sample codes on the internet how to read data in JavaScript from SharePoint. But I think that all sample codes are made only to communicate as JS add-in on SHP. But when I try to use this codes in custom widget I can't get to SHP.

This is the code I try to use:

_onPublishClick:function () {
  var hostweburl;
  var appweburl;
  $(document).ready(function () {
         //Get the URI decoded URLs.
         hostweburl = "https://server.domain.cz";
         appweburl = "https://server.domain.cz";

         // Load the SP.RequestExecutor.js file.
         $.getScript(hostweburl + "/_layouts/15/SP.RequestExecutor.js", runCrossDomainRequest);
        });

        // Build and send the HTTP request.
        function runCrossDomainRequest() {
          var executor = new SP.RequestExecutor(appweburl); 
          executor.executeAsync({
          url: appweburl + "/_api/SP.AppContextSite(@target)/web/lists?@target='" + hostweburl + "'",
          method: "GET", 
          headers: { "Accept": "application/json; odata=verbose" }, 
          success: successHandler, 
          error: errorHandler 
         });
        }

      // Get a query string value.
      // For production add-ins, you may want to use a library to handle the query string.
   
       ///////////////////////////////////////////////////////
       // Function to handle the success event.
       // Prints the host web's title to the page.
       function successHandler(data) {
           var jsonObject = JSON.parse(data.body);
           console.log(jsonObject.d.Title)
       }

       // Function to handle the error event.
       // Prints the error message to the page.
       function errorHandler(data, errorCode, errorMessage) {
           console.log("Could not complete cross-domain call: " + errorMessage)
           console.log(data)
       }‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

It jumps to the errorHandler function with this error message: Could not complete cross-domain call:  for this URL request of application (https://server.domain.cz) isn't a web of application deployed.

And when I try to print data in errorHandler function all attributes of the ResponseInfo are undefined. 

So I think that my widget can't comunicate with SHP but I don't know why.

0 Kudos