Upgrade SharePoint Sandbox Solution

There are certain scenarios where we have to deploy the sandbox solution again with new functionality (like new column in list). Since existing solution already stores crucial data, then retracting the previous version of solution will result in losing of existing data.

Let say, you have sandbox solution which is already deployed on production with version 1.0.0.0. Now you have to made some changes in list schema of the same solution. To do that, follow these steps to make changes in solution so that upgradation works well:
  1. Make changes in your list like add new column and save it.
  2. Open feature which is linked with list.
  3. Click on “Manifest” option. Expand Edit options.
  4. Replace with the below content, change ListName with you list Name and save it. Here I am upgrading the list in which I have added one more column.
    <?xml version=”1.0″ encoding=”utf-8″ ?>
    <Feature xmlns=”http://schemas.microsoft.com/sharepoint/“>
    <UpgradeActions>
    <VersionRange BeginVersion=”0.0.0.0″ EndVersion=”1.9.9.9″>
    <ApplyElementManifests>
    <ElementManifest Location=”ListName\Elements.xml” />
    </ApplyElementManifests>
    </VersionRange>
    </UpgradeActions>
    </Feature>
  5. Now open properties of same feature (press F4 when you are at feature window). There you will find Version property. Currently it is blank and you have put there any version which lies within 0.0.0.0 to 1.9.9.9
  6. Let say I am putting 1.1.0.0 and save it.
  7. Publish the solution and create wsp package.
  8. Rename the wsp package because existing solution with same name is already present.
  9. Now open Sharepoint management shell with admin account.
  10. Execute below powershell commands to add and upgrade the new solution
    1. Add-SPUserSolution -Site <sitecollectionURL> -LiteralPath <full path of sandbox solution wsp>
    2. Update-SPUserSolution -Identity <existingSolutionName> -Site $workspaceUrl -ToSolution <newSolutionName>
  11. Go to list settings and you will find new column.

Note : Follow above steps in case you have to update content type. Changes will be done at step 4 where you have to use AddContentTypeField instead of ApplyElementManifests

For more details, please follow read below articles:

SharePoint SPFile : MoveTo and CopyTo method

In SharePoint, using object model, files can moved from one folder to another folder or one library to another library within the same site collection, For this, SPFile object has two methods :

  • MoveTo(string)
  • CopyTo(string)

SPFile.MoveTo(string destinationURL)

This method moves the original file with all metadata and version history from one location to another location by providing the destination url as a parameter.

SPWeb spWeb = SPContext.Current.Site.RootWeb;
SPFileCollection allFiles = spWeb.GetFolder("Source_Folder").Files;
for (int intIndex=allFiles.Count-1; intIndex>-1; intIndex--)
{
   allFiles[intIndex].MoveTo("Destination_Folder/" + allFiles[intIndex].Name);
}

 

SPFile.CopyTo(string destinationURL)

This method copies the original file with all metadata from one location to another location by providing the destination URL as a parameter.

SPWeb spWeb = SPContext.Current.Site.RootWeb;
SPFileCollection allFiles = spWeb.GetFolder("Source_Folder").Files;
for (int intIndex=allFiles.Count-1; intIndex&gt;-1; intIndex--)
{
   allFiles[intIndex].CopyTo("Destination_Folder/" + allFiles[intIndex].Name);
}

 

Note : Both methods have one more overloaded method which accepts Boolean as parameter:

  • SPFile.MoveTo(string, boolean)
  • SPFile.CopyTo(string, boolean)

As per msdn, if this parameter is true then it will overwrite the existing file with same name in destination URL. But these methods work differently, if there is any existing file in destination URL then it is first deleted and moved to Recycle Bin and after that original file is moved or copied to destination URL.

Feature Stapling in SharePoint

I want a custom list to be created by default whenever a site is created using the Team Site template. The best way to do this is to create a Feature Stapling.

Feature Stapling can be created for the site definition that is already in use. Feature Stapling has two features.

  • Stapler feature: staples another feature to the site definition
  • Staplee feature: that which will be stapled

Steps involved:-

The following is the procedure steps to to implement Feature Stapling in SharePoint:

  • Create Empty SharePoint Project
  • Create a feature
  • Create the feature stapler
  • Create FeatureAssociation.xml
  • Deploy the solution
  • Check whether the feature stapler is activated in a web application
  • Testing

Create Empty SharePoint Project

  • Open Visual Studio 2010 by going clicking “Start” then select “All Programs” | “Microsoft Visual Studio 2010” then right-click on Microsoft Visual Studio 2010 and click on “Run as administrator”.
  • Go to the File tab, click on “New” and then click on “Project”.
  • In the New Project dialog box, expand the Visual C# node, and then select the SharePoint 2010 node.
  • In the Templates pane, select “Empty SharePoint Project”.
  • Enter the Name as FeatureStapling and then click “OK”.

featureStaplingPrj

  • Enter the local site URL for debugging, select “Deploy as a farm solution” as in the following and then click on “Finish”.

Create a feature

In this section you will see how to create a new feature and feature receiver to create a custom list in the site.

  • In the Solution Explorer, right-click on the Feature folder and then click on “Add Feature”.

featureStaplingPrj2

  • Rename the feature to “CreateListFeature”.
  • Double click and enter the title & description ofthe feature .
  • Select the scope as web from the drop down list.
  • Right-click on CreateListFeature and then click on the Add Event receiver.

featureStaplingPrj3

  • Double-click on CreateListFeature.EventReceiver.cs and replace the code with the following:
protected override void FeatureDeactivatingInternal(SPFeatureReceiverProperties properties)
{
     SPWeb web = (SPWeb)properties.Feature.Parent;
     string listUrl = SPUrlUtility.CombineUrl(web.ServerRelativeUrl, &amp;amp;quot;Lists/CertificateInformationTool&amp;amp;quot;)
     SPFolder folder = web.GetFolder(listUrl);
     if (folder.Exists)
     {
         SPList list = web.Lists[folder.ParentListId];

         list.Recycle();
     }
}
  • The Code above creates a list on the feature activation .

Create feature stapler

In this section you will see how to create a feature stapler that is used to staple the CreateListFeature to the site definition.

  • In the Solution Explorer, right-click on the Feature folder and then click on “Add Feature”.

featureStaplingPrj4

  • Rename the feature to “FeatureStapler”.

featureStaplingPrj5

  • Double-click on the feature and enter the Title and Description for the feature.

featureStaplingPrj6

  • Select the scope as WebApplication from the drop down list.

Create FeatureAssociation.xml

Now we need to create the XML file to associate the feature to the site definition.

  • In the Solution Explorer, right-click on the project, click on “Add” and then click on “New Item”.
  • Select the “Empty Element” template, enter the Name and then click on “Add”.

featureStaplingPrj7

  • Double-click on Elements.xml and replace with the ListFeature feature Id.

                 Template Name : To which site template the feature should be associated (the                                                            format should be <site template name><configuration                                                                        number>).

  • Ensure this file is added to the FeatureStapler feature (double-click on the FeatureStapler feature and see whether in the “Items in the feature” window
    the Elements.xml file is available).

featureStaplingPrj8

Deploy the solution

  • Right-click on the solution and then click on “Deploy Solution”.
  • Check whether the feature stapler is activated in the web application.

Testing

Now whenever we go to any of the site collections and look for the particular feature, it should be activated .
Thus, we can have this list on any site collection in a particular web application when we have this feature activated and vice-versa.

featureStaplingPrj9

References

https://msdn.microsoft.com/en-us/library/vs/alm/bb861862(v=office.12).aspx

SharePoint Search 2013 File Types

SharePoint Search 2013 default supports below mentioned lists of file types. The crawl component only crawls those file types which are mentioned in “File Types” section of Search Administration” page. The content processing component indexed the content of file only if built-in or third party Format Handler (iFilter) is installed in SharePoint server

File format Format ID File name extension File name extension listed on the Manage File Types page by default
Email message eml .eml Yes
Email message nws .nws Yes
HTML html .ascx Yes
HTML html .asp Yes
HTML html .aspx Yes
HTML html .css No
HTML html .hta No
HTML html .htm Yes
HTML html .html Yes
HTML html .htw No
HTML html .htx No
HTML html .jhtml No
HTML html .stm No
MHTML document mhtml .mht Yes
MHTML document mhtml .mhtml Yes
Microsoft Excel xlb .xlb No
Microsoft Excel xlc .xlc No
Microsoft Excel xls .xls Yes
Microsoft Excel xlsb .xlsb Yes
Microsoft Excel xlsm .xlsm Yes
Microsoft Excel xlsx .xlsx Yes
Microsoft Excel xlt .xlt No
Microsoft OneNote one .one No
Microsoft PowerPoint pot .pot No
Microsoft PowerPoint ppa .ppa No
Microsoft PowerPoint pps .pps No
Microsoft PowerPoint ppt .ppt Yes
Microsoft PowerPoint pptm .pptm Yes
Microsoft PowerPoint pptx .pptx Yes
Microsoft Publisher pub .pub Yes
Microsoft Word doc .doc Yes
Microsoft Word docm .docm Yes
Microsoft Word docx .docx Yes
Microsoft Word dot .dot Yes
Microsoft Word dotx .dotx Yes
Microsoft XPS xps .xps No
Open Document Chart odc .odc Yes
Open Document Presentation odp .odp Yes
Open Document Spreadsheet ods .ods Yes
Open Document Text odt .odt Yes
Outlook item msg .msg Yes
Portable Document Format pdf .pdf Yes
Rich Text Format rtf .rtf No
Text txt .asm Yes
Text txt .bat No
Text txt .c No
Text txt .cmd No
Text txt .cpp No
Text txt .csv Yes
Text txt .cxx Yes
Text txt .def Yes
Text txt .h No
Text txt .hpp No
Text txt .lnk No
Text txt .mpx No
Text txt .php No
Text txt .trf No
Text txt .txt Yes
Text txt .url No
TIFF tiff .tif No
TIFF tiff .tiff No
Visio vdw .vdw Yes
Visio vdx .vdx Yes
Visio vsd .vsd Yes
Visio vsdm .vsdm Yes
Visio vsdx .vsdx Yes
Visio vss .vss Yes
Visio vssm .vssm Yes
Visio vssx .vssx Yes
Visio vst .vst Yes
Visio vstm .vstm Yes
Visio vstx .vstx Yes
Visio vsx .vsx Yes
Visio vtx .vtx Yes
XML xml .jsp Yes
XML xml .mspx No
XML xml .rss No
XML xml .xml Yes
ZIP zip .zip Yes

Important Points:

  • The file will not crawled and parsed if it is not listed in “File Types” section and no Format Handler (iFilter) is installed respectively. For example, you had uploaded a file of type *.twbx in SharePoint site and “twbx” is not listed in “File Types” section then it was not crawled and not available in crawl logs of SharePoint search. To make it crawl, you need to add “twbx” in “File Types” section. After adding the file type, restart the SharePoint Search service and starts Full Crawl. The file will now be available in crawl logs and you can search it with filetype:twbx.
  • The content of file can only be parsed if built-in or third party iFilters are installed. To implement custom iFilter you can read this article : http://blogs.technet.com/b/sharepointdevelopersupport/archive/2013/05/13/how-to-implement-a-custom-ifilter-in-sharepoint-2013.aspx

References:

Default crawled file name extensions and parsed file types in SharePoint Server 2013

 

Create additional lookup columns using powershell in SharePoint

In SharePoint, we can create additional lookup columns through user interface but the internal name of these fields are not appropriate. Also, there is no provision to create additional fields for “User” or “UserMulti” type fields.
The below powershell can create additional columns by taking certain parameters

  1. $web : SharePoint web object
  2. $parentListName : List name in which additional lookup columns need to be created
  3. $parentCol : Lookup column internal name
  4. $childColName : Additional column name which we want to create
  5. $lookupColType : Type of additional lookup column (e.g. “Lookup”, “LookupMulti”, “User”, “UserMulti”)
  6. $showField : Field to be bind with additional column (e.g. “ID”)
function CreateAdditionalLookupColumns([Microsoft.SharePoint.SPWeb] $web, [string] $parentListName, [string] $parentCol, [string] $childColName, [string] $lookupColType, [string] $showField)
{
$parentList = $web.Lists.item($parentListName)

$fieldLookup = $parentList.Fields.GetFieldByInternalName($parentCol)
#write-host $fieldLookup.SchemaXml `r`n

if($lookupColType -eq "Lookup" -or $lookupColType -eq "User")
{
$schemaXML = ''
$schemaXML = [string]::Format($schemaXML,$childColName, $fieldLookup.Id, [guid]::NewGuid(), $childColName, $lookupColType, $fieldLookup.LookupList, $showField, $childColName, $fieldLookup.SourceID);
}
elseif($lookupColType -eq "UserMulti")
{
$schemaXML = ''
$schemaXML = [string]::Format($schemaXML,$childColName, $fieldLookup.Id, [guid]::NewGuid(), $childColName, $lookupColType, $fieldLookup.LookupList, $showField, $childColName, $fieldLookup.SourceID);
}
elseif($lookupColType -eq "LookupMulti")
{
$schemaXML = ''
$schemaXML = [string]::Format($schemaXML,$childColName, $fieldLookup.Id, [guid]::NewGuid(), $childColName, $lookupColType, $fieldLookup.LookupList, $showField, $childColName, $fieldLookup.SourceID);
}

#write-host $schemaXML `r`n

write-host "---------" $parentListName "----------------------" `r`n

$fieldExist = $parentList.Fields.GetFieldByInternalName($childColName)
if($fieldExist -ne $null)
{
write-host "field deleted : " $fieldExist `r`n
$parentList.Fields.Delete($childColName)
}

$fieldNew = $parentList.Fields.AddFieldAsXml($schemaXML);
write-host "field created : " $fieldNew `r`n
write-host "-----------------------------------------------------" `r`n
}

$web = Get-SPWeb "http://siteURL"    # Your site URL

CreateAdditionalLookupColumns $web "ParentList" "ParentLookupColumnName" "AdditionalLookupColumnName" "Lookup" "ID"

CreateAdditionalLookupColumns $web "ParentList" "ParentLookupColumnName" "AdditionalLookupColumnName" "User" "Name"