How to access data in Host Web from SharePoint 2013 App

When we build SharePoint 2013 Apps, one of the common scenario that we will have is to read the data from Host Web.  This presents a classical Cross-Domain call scenario (where the fully-qualified domain for App Web and Host Web are different). By default, when we access the Host Web resources from App Web either using Java Script Model or Client Object Model, we’d get an access denied error. This fix for this scenario is to leverage javascript client-side solution in the form of (SP.RequestExecutor.JS) file. The cross domain libraries helps us to interact with more one domain in the SharePoint 2013 App through the Proxy. Let’s jump on to the steps.

Open Visual Studio 2013

File –> New –> Project –> Apps for SharePoint 2013 –> SharePoint hosted apps and name the App as ‘ReadHostWeb;

Set the site for debugging, in this case I’m using my Office 365 developer tenant ‘https://mytenant.sharepoint.com’

I’ve created an Announcement list in my Host Web and it has three Columns Title, Body and Version.

Open Default.aspx and paste the following snippet of code inside

 

<div id="ListFieldDiv"></div>

    <script type="text/javascript">
        var HostWebUrl;
        var AppWebUrl;
        var ListFieldHtml;

        // Load the required SharePoint libraries
        $(document).ready(function () {
            //Get the URI decoded URLs.
            HostWebUrl =
                decodeURIComponent(
                    RetrieveQueryStringParameter("SPHostUrl")
            );
            AppWebUrl =
                decodeURIComponent(
                    RetrieveQueryStringParameter("SPAppWebUrl")
            );

            
            var scriptbase = HostWebUrl + "/_layouts/15/";

            
            $.getScript(scriptbase + "SP.RequestExecutor.js", execCrossDomainRequest);
            
        });


        
        
        function execCrossDomainRequest() {
            
            var executor = new SP.RequestExecutor(AppWebUrl);

            
            executor.executeAsync(
    {
        url:
            AppWebUrl +
            "/_api/SP.AppContextSite(@target)/web/lists('5fabe2a3-7a5f-4444-ba3e-a684558d1e27')/fields?@target='" +
            HostWebUrl + "'",
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: successHandler,
        error: errorHandler
    }
);

        }

        
        function successHandler(data) {

            var jsonObject = JSON.parse(data.body);
            var results = jsonObject.d.results;

            for (i = 0; i < results.length; i++)
            {
                if (results[i].Hidden == false) {

                    

                    if ((results[i].TypeDisplayName == "Single line of text") || (results[i].TypeDisplayName == "Multiple lines of text")) {

                        
                        if (results[i].Title == "undefined" || results[i].Title == "Version")
                        {


                        }
                        else
                        {
                            ListFieldHtml = ListFieldHtml + "<p>" + results[i].Title;

                        }

                        
                    }
                }
                

            }
            
            document.getElementById("ListFieldDiv").innerHTML = ListFieldHtml;

            
            


            
        }

       
        function errorHandler(data, errorCode, errorMessage) {
            document.getElementById("AnnouncementsDiv").innerText =
                "Could not complete cross-domain call " + errorMessage;
        }

       
        function RetrieveQueryStringParameter(ParamsforRetrieval) {
            var params =
                document.URL.split("?")[1].split("&");
            var strParams = "";
            for (var i = 0; i < params.length; i = i + 1) {
                var singleParam = params[i].split("=");
                if (singleParam[0] == ParamsforRetrieval)
                    return singleParam[1];
            }
        }
        </script>

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

 image

 Subscribe to my blog

Windows Azure Queue vs Service Bus Queuue

In Microsoft Azure, we have two implementations of Queues, Windows Azure Queues and Service Bus Queues. Both the Queues are the internally implemented using Message Queuing service offered on Windows Azure.

We should prefer Windows Azure Queues for the following scenarios:-

  • When the application needs to store over 5 GB worth of messages in a Queue and the life time of messages are shorter than 7 days
  • When server-side log is required for all transactions executed against Queues
  • When the application requires flexible leasing to process messages

We should prefer Azure Service Bus Queues for the following scenario :-

  • When the application requires full integration with .NET WCF
  • When message batches need to be published and consumed
  • When the message size handled by the application is between 64 KB and 256 KB
  • When the application requires “At most once” guaranteed delivery without the need to build additional infrastructure components
  • When the application requires First-In-First-Out (FIFO) delivery
  • When the Queue Size does not exceed 5 GB
  • When role-based access to Queue is required
  • When the application requires Automatic Duplicate detection
  • When the application Requires Atomicity and Transactional behavior when sending or receiving multiple messages from Queue
  • When the message retrieval does not require polling
  • When the Time-to-Live (TTL) characteristics of application-specific workload can exceed 7 day window

More detailed guidance, soft limits, Thresholds is given in http://msdn.microsoft.com/en-us/library/hh767287.aspx

Deploying SharePoint 2013 on Microsoft Azure using IAAS–Part 3

This post is continuation to my previous post Deploying SharePoint 2013 on Azure using IAAS – Part 2. In this article, we’ll see how to install SQL 2012 RTM.

Provisioning, Configuring and Setup of SQL Server 2012 VM

New à Virtual Machine à From Gallery à SQL Server 2012 Enterprise SP1.

clip_image002

clip_image004

clip_image006

The next step is to allocate the empty disk space of 50 GB space to the primary VM ‘SP2013-SQL1’ . Refer the part 1 of this article to know more about the empty disk allocation for various VMs.

Log-in to the SP2013-SQL1 VM and join the server to the Domain (techguru.com). Use the account ‘sp_install’ for joining the server to the domain.

clip_image008

On the F drive, create 3 folders namely Data, Backup and Log.

On Start menu àSQL Server 2012 Management Studio.

Right click on the default database instance à properties.

In the Server Properties click database settings. On the default database location settings, change the location of Data, Log and Backup to the corresponding folders on F drive.

clip_image010

The next step is to grant the service account sp_farm_db with Sys Admin rights.

Security à Logins à New login à sp_farm_db@techguru.com.

clip_image012

In the Server Roles à Select sysadmin

clip_image014

The next step is to grant NT AUTHORITYSYSTEM account with relevant permissions.

Security à Logins à New login à NT AUTHORITYSYSTEM.

clip_image016

Right click on NT AuthoritySystem à Properties à Securables.

Grant these 3 permissions ‘Alter any availability group’, ‘Connect SQL’ and ‘View server state’ to the account NT AuthoritySystem.

Now we need to unblock the ports 1433 (default SQL Server Ports) and 5022 (high availability port).

Start à Windows Firewall with advanced security.

Click à Inbound rules.

Action pane à New Rule.

On the Rule Type à Program à Next.

On the Program Page à Set Program Path as %ProgramFiles%Microsoft SQL ServerMSSQLBinnsqlservr.exe

clip_image018

On the Action Page à Allow Connection.

On the profile page à Keep defaults.

Name the rules ‘SQL Ports Rule’.

Setup of back-up SQL Server

clip_image020

clip_image022

Then allocate the empty disk space of 50 GB. This completes the part 3 of the series. In the next post, we’ll see how to configure SQL Server 2012 Always On.

 Subscribe to my blog