Thursday, March 16, 2017

Add Web Application property bag using Power Shell and use them in SharePoint timer job

Introduction

SharePoint Web Application level property bag is used to store configuration setting data and information. I make use of the property bag when I write a timer job.

I store value in property bag as key, value using power shell and using C# server object model I retrieve and make use of it.

Set Web Application Level Property Bag

You can create a function in Power shell

 function SetWebApplicationPropertyBag ($webAppUrl, $key, $value) {

	$Web = Get-SPWebApplication -Identity $webAppUrl
	$Web.Properties.Add($key,$value) #add property bag
	$Web.Update()

	Write-host "Created new Property:" $Web.Properties[$key]
 
 }

And you can call the function like this.

$webAppUrl ="Web Application url goes here"
$PropertyName1 ="Key"
$PropertyValue1 ="Value"

SetWebApplicationPropertyBag $webAppUrl $PropertyName1 $PropertyValue1

You have to change the values of $webAppUrl, $PropertyName1 and #PropertyValue1.

Copy paste code in SharePoint Management shell and your web application property will be created.

Retrieve property bag value in SharePoint Timer Job

Since SharePoint Timer Jobs are created with the feature, in Web Application level scope, we can use the below code to retrieve the data.

var value = WebApplication.Properties["Key"].ToString();

You can make use of value and do operations.

Remove Property Bag

You can use below code to remove a value from property bag.

$Web = Get-SPWebApplication -Identity "Web Application Url"

$Web.Properties.Remove("Key")

$Web.Update()

Thursday, March 9, 2017

Change the Language of SharePoint List Template

Problem Background

Recently I had to save a list as “List Template” from my client’s SharePoint Online site (which the language is configured to French) and upload to my SharePoint site (the language is English) and create a list using that particular List Template.

However, the list template was not displayed when I try to create a new list by navigating to “Add an App”. This is because of the language is not the same.

Solution

So, what I did was to change the language of the list template.

Step 1

Download the List Template from SharePoint List Template Gallery. The extension of the list template file would be “.stp”.

Step 2

Change the extension from “.stp” to “.cab” as given blow.

image 

Step 3

Extract the file and you will be getting a “manifest.xml” file

image

Step 4

Open the .xml file a note page and change the language to your desired language number and save it.

image

You can get the language ID list from the below url.

https://technet.microsoft.com/en-us/library/ff463597.aspx

Step 5

Open PowerShell and navigate to the folder where your manifest.xml is located and run the below script.

  
 makecab manifest.xml [Name of the list template].stp

My one looks as below.

image

Hit Enter and your List Template will be created.

image

Conclusion

Now your list template is created with new Language. You can upload to List Template Gallery and you can start creating the list.

image

image

Wednesday, March 1, 2017

Basic operations in Host Web from Add-in Web using PnP JavaScript Core Library

Introduction

My my previous blogposts I discussed on

  1. Use sp-pnp-js with SharePoint hosted Add-in
  2. Communicate with Host Web from Add-in Web through PnP JavaScript Core Library

This post will be a continue of the above two posts. In this post we will be addressing the basic operations on SharePoint list items.

Permission

In order to do operations on the list on host web we need to give permission to the List. In our case we have to set the permission “Scope” to “List” and “Permission” as “Manage”. Please refer the image below.

Click on AppManifest.xml –> Permission

Set “Scope” to “List” and “Permission” to “Manage”

image_thumb[1]

The addinWeb will be the Add-in web url and the hostWeb will be the Host Web url. You may download the solution from the link given at the bottom and refer for more.


Add new Item to List

  
    $pnp.sp.crossDomainWeb(addinWeb, hostWeb).lists.getByTitle("List1").items.add({
        Title: "Title"
    }).then(function (result) {
        alert("Item Added");
        //"result" will return couple of properties of newly created item. We can make use of them here
        console.log(result.data);
        console.log(result.data.Id);
    }).catch(function (err) {
        alert(err);
    });


Update existing Item

  
    //Get the item By Id and call the update
    $pnp.sp.crossDomainWeb(addinWeb, hostWeb).lists.getByTitle("List1").items.getById(2).update({
        Title: "Title Update"
    }).then(function (result) {
        alert("Item Updated");
        $("#message").html("Item Updated");
        //"result" will return couple of properties of updated item.
        console.log(result.data);
        console.log(result.data.Id);
    }).catch(function (err) {
        alert(err);
    });


Delete a exiting Item

  
 //Get the item By Id and call delete
    $pnp.sp.crossDomainWeb(addinWeb, hostWeb).lists.getByTitle("List1").items.getById(2).delete().then(function (result) {
        alert("Item Deleted");
        $("#message").html("Item Updated");
    }).catch(function (err) {
        alert(err);
    });

Using the above code snippet you can do the basic operations.

Conclusion

Hit F5 and you will prompt to Trust and select the List you want  give the permission to.

Select the List name from the dropdown and Click on “Trust”.


image_thumb[3]

You can download the code from the below.

https://drive.google.com/file/d/0ByEnOE8DAdvhSUhHWEJuTzBTUm8/view?usp=sharing

Reference

https://github.com/SharePoint/PnP-JS-Core/wiki/Working-With:-Items