Upgrading SharePoint App


Whenever you make changes and redeploy the app, data that were created previously by the app will be lost. To make changes in the app without losing the data, you need to upgrade the app.

Let us see how to upgrade the app:

  • Open AppManifest.xml and increment the version number by 1 whenever a change is made
  • In my case, version is 1.0.0.2. Hence I am changing it to 1.0.0.3 and publishing the app as shown below:

Publishing app

  • Click on Finish in Publish summary.

Publish summary

  • Explorer window opens as depicted below:

Published app version

  • Copy the location of the file and go to the developer site
  • You can see the list of apps in App Testing. Select the app and click on upgrade an app

Upgrade

  • Click on Upload and browse the file from copied location

Upgrade - Upload

  • Click on Upgrade

Upgrad - Complete

  • You can see the upgrade progress as shown below:

Upgradeinprogress

  • Once upgraded, the version is changed to 1.0.0.3 and if you check for the previous data, it actually exists.

That’s it!

Reference: http://www.c-sharpcorner.com/article/upgrade-sharepoint-hosted-add-insapps-in-sharepoint-2013/

 

Advertisements

PART 2 : SharePoint-Hosted App


Have you read PART 1? Cool. Continue reading how to create an app in host web.

Let us see the same create operation for better understanding. We need two URLS for this:

  • HostURL
  • AppWebURL

To retrieve the URLS, QueryStringParameter() is generally used. But this does not work in all the cases especially when we move from one page to another in app web. So, let’s use the below one:

  • appWebUrl = window.location.protocol+”//”+window.location.host+_spPageContextInfo.webServerRelativeUrl;
  • hostUrl = _spPageContextInfo.siteAbsoluteUrl;

To change the context site from app web to other sites, AppContextSite() is used. To change the current context(i.e, app web) to host web, pass it as
var appctx = new SP.AppContextSite(ctx, hostUrl);

Let’s combine all these with previous code. Here you go:

var appWebUrl;
var hostUrl;
$(document).ready(function ()
 {
appWebUrl = window.location.protocol+"//"+window.location.host+_spPageContextInfo.webServerRelativeUrl;
hostUrl = _spPageContextInfo.siteAbsoluteUrl;
 $("#create").on('click', function ()
 {
                createList();
  });
});
//create List
function createList()
 {
    var listTitle = $("#listName").val();
    var ctx = new SP.ClientContext(appWebUrl);
    var appctx = new SP.AppContextSite(ctx, hostUrl);
    var oweb = appctx.get_web();
    var listCreationInfo = new SP.ListCreationInformation();
    listCreationInfo.set_title(listTitle);
    listCreationInfo.set_templateType(SP.ListTemplateType.genericList);
    oweb.get_lists().add(listCreationInfo);
    ctx.executeQueryAsync(onQuerySucceeded, onQueryFailed);
}
function onQuerySucceeded() 
{
    alert("List Created");
}
function onQueryFailed(sender, args) {
    alert("Operation Failed" +"\n"+ args.get_message());
}

After deploying the app, when you create the list, the below error is shown

Access Denied for Host Web

  • This is because to access or create resource in host web, we need to give FULL CONTROL permission for web and list. To give permission, open AppManifest.xml, move to permission tab and give the permission as shown below:

Fullconrol Permission

  • Now when you deploy it, you will get the below page:

Trust the app

  • Click on trust and create a list using the app. List is created in the host web and can be seen in the site contents page.

If you have noted that when you deployed the app again after making some changes, the data created previously were lost. Continue reading the next article, Upgrading SharePoint app to learn how to deploy the app without losing the data.

PART 1: SharePoint-Hosted App


Apps for SharePoint are also called as SharePoint Add-ins. In SharePoint 2013, everything is considered as an app. Lists, libraries are all called as apps. Apps can be hosted

  • In SharePoint farm (SharePoint-hosted)
  • In separate server(Provider-hosted)
  • In cloud(Auto-hosted)

SharePoint-Hosted App:

It includes only SharePoint components. SharePoint Site where the app is installed is called Host web. All the resources such as list, libraries are in isolated sub site called App web.

Let us create a SharePoint-hosted app to create a list in SharePoint On-Premise environment.

  • Run Visual Studio as administrator and click on File->New->Project

New Project

  • Select Apps for SharePoint 2013 and give a name(say, createListinHost) and hit ok button

Host

  • Next, Specify the site url ( say,Developer site), select the type as SharePoint Hosted and click on Finish

SharePoint Hosted app

  • App is created and default aspx page is shown.

Default aspx

  • Replace the highlighted code with the below one to create a textbox and button for create operation.

<label for=”listName”>List Name to be created</label>
<input type=”text” id=”listName” Placeholder=”Enter the listname”/>
<input type=”button” id=”create” value=”Create”/>

  • Branding(Optional):
    • You can change the title of the page in
      <asp:Content ContentPlaceHolderID=”PlaceHolderPageTitleInTitleArea” runat=”server”> List Creation </asp:Content
    • You change the icon for app in AppManifest.xml. Open AppManifest and change the image for Icon. Image should be of 96*96 pixel

App Icon

  • To insert create functionality in this app, expand Scripts, open App.js and insert the below code:
$(document).ready(function () 
{ 
    $("#create").on('click', function () {
                createList();
    });
});
//create List
function createList()
{
    var listTitle = $("#listName").val();
    var ctx = SP.ClientContext.get_current();
    var oweb = ctx.get_web();
    var listCreationInfo = new SP.ListCreationInformation();
    listCreationInfo.set_title(listTitle);
    listCreationInfo.set_templateType(SP.ListTemplateType.genericList);
    oweb.get_lists().add(listCreationInfo);
    ctx.executeQueryAsync(onQuerySucceeded, onQueryFailed);
}
function onQuerySucceeded() {
    alert("List Created");
}
function onQueryFailed(sender, args) {
    alert("Operation Failed" +"\n"+ args.get_message());
}
  • Press F5 or click on Solution->Deploy to deploy the code. Default aspx page will be opened in browser. If not go to the site->Site Contents and click on the app, “createListinHost”

App in Site Contents

  • Create a list say, Sample

 

Below page is shown where you can see that sample list created under “createListinHost”

Sample list settings

Site lists and libraries

Now, question arises. Should all the resources reside in app web only? What If I need to create a resource or access a resource that is in host web? Can I do it? Yes, you can create and access resources in host web. Continue Reading in PART 2