4

I am very familiar with the feature upgrade toolkit from Chris O Brien. I also read that in Apps you can handle an Upgrade event or something, but yet, there are no examples about this.

The scenario is the following: 1. I create an app with 3 content types and 3 lists for version 1.0

Lets suppose in 3 months the user wants to add columns to the existing content types. If there is no feature upgrading events in apps, what would be the best approach to do it?

1 Answer 1

2

Use PowerShell:

param
(
    [string]$Web = $(throw '- Need a SharePoint web site URL (e.g. "http://mysp15site/")'),
[string]$Source = "ObjectModel"
)

Write-Host -ForegroundColor White "------------------"
Write-Host -ForegroundColor White "| App Upgrader |"
Write-Host -ForegroundColor White "------------------"
Write-Host -ForegroundColor White "- "

#Global vars
$AppPackageName = "MyCustomAppPackage.app";
$AppName = "My Custom App";

#Loads powershell settings
Write-Host -ForegroundColor White "- Load Powershell context.."
$0 = $myInvocation.MyCommand.Definition
$dp0 = [System.IO.Path]::GetDirectoryName($0)

#Loads the SharePoint snapin
Write-Host -ForegroundColor White "- Load SharePoint context.."
$ver = $host | select version
if ($ver.Version.Major -gt 1) {$host.Runspace.ThreadOptions = "ReuseThread"} 
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
    Add-PSSnapin "Microsoft.SharePoint.PowerShell";
}
[void][System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") 

#Imports the App package
Write-Host -ForegroundColor White "- Import app package '$AppPackageName'..."
$appPath = $dp0 + "\" + $AppPackageName;
if ($Source.Equals("ObjectModel", [System.StringComparison]::InvariantCultureIgnoreCase)) {
$sourceApp = ([microsoft.sharepoint.administration.spappsource]::ObjectModel);
}
elseif ($Source.Equals("Marketplace", [System.StringComparison]::InvariantCultureIgnoreCase)) {
$sourceApp = ([microsoft.sharepoint.administration.spappsource]::Marketplace);
}
elseif ($Source.Equals("CorporateCatalog", [System.StringComparison]::InvariantCultureIgnoreCase)) {
$sourceApp = ([microsoft.sharepoint.administration.spappsource]::CorporateCatalog);
}
elseif ($Source.Equals("DeveloperSite", [System.StringComparison]::InvariantCultureIgnoreCase)) {
$sourceApp = ([microsoft.sharepoint.administration.spappsource]::DeveloperSite);
}
elseif ($Source.Equals("RemoteObjectModel", [System.StringComparison]::InvariantCultureIgnoreCase)) {
$sourceApp = ([microsoft.sharepoint.administration.spappsource]::RemoteObjectModel);
}

$spapp = Import-SPAppPackage -Path "$appPath" -Site $Web -Source $sourceApp -Confirm:$false -ErrorAction SilentlyContinue -ErrorVariable err;
if ($err -or ($spapp -eq $null)) 
{
Write-Host -ForegroundColor Yellow "- An error occured during app import !"
throw $err;
}
Write-Host -ForegroundColor White "- Package imported with success."

#Installs the App
Write-Host -ForegroundColor White "- Updating the APP in web site..."
$appInstance = Get-SPAppInstance -Web $Web | where-object {$_.Title -eq $AppName};
$app = Update-SPAppInstance -Identity $appInstance -App $spapp -Confirm:$false -ErrorAction SilentlyContinue -ErrorVariable err;
if ($err) {
Write-Host -ForegroundColor Yellow "- An error occured during app update !"
throw $err;
}
$AppName = $app.Title;
Write-Host -ForegroundColor White "- App registered, please wait during update..."
$appInstance = Get-SPAppInstance -Web $Web | where-object {$_.Title -eq $AppName};
$counter = 1;
$maximum = 150;
$sleeptime = 2;
Write-Host -ForegroundColor White "- Please wait..." -NoNewline;
while (($appInstance.Status -eq ([Microsoft.SharePoint.Administration.SPAppInstanceStatus]::Upgrading)) -and ($counter -lt $maximum))
{
Write-Host -ForegroundColor White "." -NoNewline;
sleep $sleeptime;
$counter++;
$appInstance = Get-SPAppInstance -Web $Web | where-object {$_.Title -eq $AppName} 
}
Write-Host -ForegroundColor White ".";

if ($appInstance.Status -eq [Microsoft.SharePoint.Administration.SPAppInstanceStatus]::Installed) {
Write-Host -ForegroundColor White "- The App was successfully upgrade.";
$appUrl = $appInstance.AppWebFullUrl;
Write-Host -ForegroundColor White "- The App is available at '$appUrl'.";
Write-Host -ForegroundColor White "- "
}
else {
Write-Host -ForegroundColor Yellow "- An unknown error has occured during app update. Read SharePoint log for more information.";
}

Reference: Scripts to install, update or uninstall a SharePoint 2013 App with PowerShell

But to "add columns to the existing content type" I think you need to do the same as in 2010, with PowerShell. I havn't tried this myself - but this is what I would try:

function AddFieldToContentType([Microsoft.SharePoint.SPWeb] $web, [string]$contentTypeName, [string]$xmlSchema)
{
    $fieldName = $web.Fields.AddFieldAsXml($xmlSchema)
    $web.Update()

    $field = $web.Fields[$fieldName]
    $ct = $web.ContentTypes[$contentTypeName]
    $link = new-object Microsoft.SharePoint.SPFieldLink $field
    $ct.FieldLinks.Add($link)
    $ct.Update($true)
}

[Microsoft.SharePoint.SPWeb]$web = get-spweb -Identity http://somewhere
[xml]$xml = 'my field schema truncated for readability'

AddFieldToContentType $web "my content type name" $xml

Reference: Accepted answer to the question: Adding a site column and add it to a content type from PowerShell issue

3
  • this updates an app, but where does it say where to add columns to a content type in an app, how does this really work, does it work the same as the feature upgrade toolkit from Chris O Brien?, can I set a begin version and end version and decide what to do on each upgrade ? Commented Apr 30, 2013 at 5:24
  • @LuisValenciaMunoz True that! I updated my answer to your question. If it works the same way as Chris O Briens toolkit, I wouldn't know - since I havn't used it (yet).
    – Benny Skogberg
    Commented Apr 30, 2013 at 7:29
  • I doubt that it works the same way as sharepoint apps are provisioned in a different web that is not part from sharepoint itself. I will wait to see if someone else has a diferent answer to this. Commented Apr 30, 2013 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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