SharePoint Online: Working With List Content Types Using PowerShell

In this article we will discuss the configuration of some of the important operations on Content Type for SharePoint Online Lists. Though I have taken only a handful of operations to showcase in this demo, but remember there are lot more to explore.

Operation: How to enable “Content Types” for List

We can see this setting available under “Advanced Settings” for the list as shown below-

1

We can play with this setting using PowerShell as described below-

2

In Step 1 we will get the object reference to the respective list by calling “GetByTitle” method

In Step 2 we will set “ContentTypesEnabled” to True

In Step 3 we will update the list property by calling “Update” method

In Step 4 we will send the batch request to SharePoint Server for processing by calling “ExecuteQuery” method

In Step 5 we will display success message to the Users if Step 4 executes successfully

In Step 6 we will call function that we have explained in Step 1-5

3

Once this script executes successfully we can see list setting updated by navigating “Advanced Settings” of the list as shown below-

4

Operation: How to “Add Existing Content Type To List”

We can see this setting available under “List Settings” for the list as shown below-

5

We can play with this setting using PowerShell as described below-

6

In Step 1 we will get the object reference to the respective Web

In Step 2 we will get the object reference to the respective list by calling “GetByTitle” method

In Step 3 we will get the object reference to the respective list by calling “GetById” method on Content Types collection

In Step 4 we will send the batch request to SharePoint Server for processing by calling “ExecuteQuery” method

In Step 5 we will display success message to the Users if Step 4 executes successfully

In Step 6 we will call function that we have explained in Step 1-5

7

Once this script executes successfully we can see a new content type added by navigating “List Settings” of the list as shown below-

We can notice the new content type added to the List Content Types Collection

8

Operation: How to “Get List of All Content Types”

We can see this setting available under “List Settings” for the list as shown below-

9

We can play with this setting using PowerShell as described below-

10

In Step 1 we will get the object reference to the respective Web in context of which this code is executing

In Step 2 we will get the object reference to the respective list by calling “GetByTitle” method

In Step 3 we will get List Content Types Collection

In Step 4 we call the “Load” function to retrieve List Content Types Collection properties from server

In Step 5 we will send the batch request to SharePoint Server for processing by calling “ExecuteQuery” method

In Step 6 & 7 we will loop through the Content Types Collection and display relevant properties (ex. Name, ID and so on) from the collection

In Step 8 we will call function that we have explained in Step 1-7

11

Once this script executes successfully we can see list of content types by navigating “List Settings” of the list as shown below-

12

Operation: How to “Delete Existing Content Types” From List

We can see the List of Content Types already added to List under “List Settings” as shown below-

12

We can delete any Content Type from list by using PowerShell by using PowerShell as described below-

13

In Step 1 we will get the object reference to the respective Web in context of which this code is executing

In Step 2 we will get the object reference to the respective list by calling “GetByTitle” method

In Step 3 we will get List Content Types Collection

In Step 4 we call the “Load” function to retrieve List Content Types Collection properties from server

In Step 5 we will send the batch request to SharePoint Server for processing by calling “ExecuteQuery” method

In Step 6 we will loop through the Content Types Collection

In Step 7 we will look for required content types that are intended to delete. In this example we will compare the Content Type Name to ensure that only intended content type are deleted

In Step 8 we will call “DeleteObject” method to delete the respective content type from Content Types Collection of List

In Step 9 we will send the batch request to SharePoint Server for processing by calling “ExecuteQuery” method

In Step 10 we will display success message after Step 9 got executed successfully

In Step 11 we will call function that we have explained in Step 1-10

14

Once this script is executed successfully the respective content type will be deleted from Content Types List and this can we verified from the UI by navigating through “List Settings”

15

That is all for this demo.

Hope you find it helpful.

Advertisements

SharePoint Online: Working With List Fields Using PowerShell

In this article we will discuss the operations on List Fields (Columns), which involves Getting All Columns, Adding New Columns, Updating Existing Columns and so on.

To start with this demo we will start with a list called “Products” and perform all operations on this list.

1

Operation: How to Add New Columns To List

We can add a new column to the list by making use of the following code.

2

In Step 1 we will get the object reference to the current Web using Client Context properties

In Step 2 we will get the object reference to the respective list by calling “GetByTitle” method

In Step 3 we will define the xml of the List Column schema. You can get this XML by prototyping the list using SharePoint UI and then by using SharePoint Client Browser to look for Schema XML for Lists & Fields.

In Step 4 we will call “AddFieldAsXml” method to add field as xml schema to the list

In Step 5 we will send the batch request to SharePoint Server for processing by calling “ExecuteQuery” method

In Step 6 we will display the success message to the users

In Step 7 we will call function that we have explained in Step 1-5

We can see this field added to the list by browsing the list

4

We can see the details of the new column (Datatype and others) by browsing List Settings

5

Operation: How Get All Columns of List

We can get all columns used in a list by making use of the following code.

6

In Step 1 we will get the object reference to the current Web using Client Context properties

In Step 2 we will get the object reference to the respective list by calling “GetByTitle” method

In Step 3 we will get the object reference to Fields collection of the list

In Step 4 we call the “Load” function to retrieve Fields collection properties from server

In Step 5 we will send the batch request to SharePoint Server for processing by calling “ExecuteQuery” method

In Step 6 we will loop through the collection and display Field details to the users

In Step 7 we will call function that we have explained in Step 1-6

We can see the fields collection to the list by browsing the list

7

Operation: How to Update column of List

Let’s consider that we have to add little description the field Title, which is blank currently

8

We can update an existing column to the list by making use of the following code.

9

In Step 1 we will get the object reference to the current Web using Client Context properties

In Step 2 we will get the object reference to the respective list by calling “GetByTitle” method

In Step 3 we will get the object reference to the respective field by calling “GetByTitle” method

In Step 4 we will set the Description property of List object with the required value

In Step 5 we will call “Update” method to save these changes back to SharePoint List

In Step 6 we will call “Load” method to retrieve updated properties (Description) of the field from Server

In Step 7 we will send the batch request to SharePoint Server for processing by calling “ExecuteQuery” method

In Step 8 we will display Field details to the users to the users

In Step 7 we will call function that we have explained in Step 1-8

10

We can see description of Title field is update to the list by browsing the field properties under list settings

11

Operation: How To Add Existing Site Columns To List

Since we are planning to add an existing Site Column to the list, it is necessary to ensure the existence of Site Column. We can verify this by navigating “Site Settings > Site Columns”.

For this demo I already have added a Site Column “ProductOwner” that we can see under “Custom Columns” group as shown below-

12

And we can also verify the list settings to ensure that “ProductOwner” Column is not added to the list earlier

13

Now we will look into code to add existing Site Column to the list as explained below-

14

In Step 1 we will get the object reference to the current Web using Client Context properties

In Step 2 we will get the object reference to the respective list by calling “GetByTitle” method

In Step 3 we will get the object reference to Fields collection of the Web. It is important to note that Site Columns are the part of Web Fields Collection not List Fields Collection. So we have to make use of Web object reference to look for existing Site Columns.

In Step 4 we call the “Add” function on “Fields” Collection of the list to add the reference of the Site Column from Step 3

In Step 5 we will send the batch request to SharePoint Server for processing by calling “ExecuteQuery” method

In Step 6 we will call function that we have explained in Step 1-5

We can see the field collection to the list by browsing the list settings-

16

We can further look into column details by clicking it

17

Operation: How Set Default Value For Field

We can set default values to SharePoint List Fields programmatically by using the following code as explained below-

18

In Step 1 we will get the object reference to the current Web using Client Context properties

In Step 2 we will get the object reference to the respective list by calling “GetByTitle” method

In Step 3 we will get the object reference to Fields collection of the list

In Step 4 we will call “DefaultValue” property of Field Object and assign it a value of our choice

In Step 5 we will call “Update” method of Field Object, which will update the “DefaultValue” property back into database

In Step 6 we will send the batch request to SharePoint Server for processing by calling “ExecuteQuery” method

In Step 7 we will display the a success message which is informing users about the status of operation

In Step 8 we will call function that we have explained in Step 1-7

19

We can see this value shown as default value whenever a new Item has been created (programmatically or using browser) as shown below-

20

That is all for this demo.

Hope you find it helpful.

SharePoint Online: Working with Lists Using PowerShell

In this article we will discuss SharePoint List operations using PowerShell for SharePoint Online.

To start with this demo, I have created a demo SharePoint Online Site that I will use to perform SharePoint operations in this and few of the upcoming articles in this series.

1

In case you are not aware of on how to start with PowerShell development for SharePoint Online, I would rather recommend you to first go through one of my previous articles. This article will help you to setup development environment and get you started.

SharePoint Online: How To Install SharePoint Online Management Shell

Provide SharePoint Online Site credential when it is asked for

2

Operation: How To Get Lists Collection

3

In Step 1 we will initialize Web Object using SharePoint Client Context

In Step 2 we initialize SharePoint Lists Collection

In Step 3 we call the “Load” function to retrieve SharePoint List Collection properties from server

In Step 4 we call “ExecuteQuery” method to send the request to SharePoint Server

In Step 5 we iterate the lists collection returned back from the server and display required list properties as output

In Step 6 we call function that we have explained in Step 1-5

And here is the output for this operation

4.1

Operation: How To Add New List

4

In Step 1 we will initialize “ListCreationInformation” object which allows to specify the metadata details for this new list

In Step 2 we will specify Title for new list

In Step 3 we will specify Description for new list

In Step 4 we will specify List Template for new list. In this example we will use Custom List Template

In Step 5 we will call “Add” method of “Lists” collection for SharePoint Web

In Step 6 we will call “Load” method, to retrieve the properties of list object from Server

In Step 7 we call “ExecuteQuery” method to send the request to SharePoint Server

In Step 8 we call function that we have explained in Step 1-7

On successful execution of Step 1-7 we will get list added to SharePoint and Internal ID of list as Output

And here is the output for this operation

5

We can also see to the SharePoint Site to ensure that a new list has been added

67

Operation: How To Update Existing List

8

In Step 1 we will initialize “List” object which allows to modify the metadata details for this existing list

In Step 2 we will update the description of the existing list

In Step 3 we will call “Update” function of list object and then call “Load” method, to retrieve the properties of list object from Server

In Step 4 we call “ExecuteQuery” method to send the request to SharePoint Server

In Step 5 we will retrieve the list description to ensure that it is updated successfully

In Step 6 we call function that we have explained in Step 1-6

And here is the output for this operation

9

We can also see to the SharePoint Site to ensure that the list description has been updated

10

Operation: How To Delete Existing List

11

In Step 1 we will initialize “List” object by calling a method “GetByTitle” on the Web Object

In Step 2 we will call “DeleteObject” method on List Object that will delete the reference of respective list from lists collection

In Step 3 we call “ExecuteQuery” method to send the request to SharePoint Server

In Step 4 we will display success message to inform successfully deletion of the list

In Step 5 we call function that we have explained in Step 1-4

And here is the output for this operation

12

We can also see to the SharePoint Site to ensure that the list has been deleted successfully from SharePoint

13

That is all for this demo.

Stay tuned for the upcoming articles in this series.

Hope you find it helpful.

PowerShell Automation: How To Send Email with HTML Body Containing Inline Images

During of a recent assignment I get across an automation requirement to send formatted emails daily to a set of users.

The email content involved the HTML formatting with lot of images as a part of layout. I have appointed PowerShell as automation technology to get this done.

I found worth writing a small article to explain the code (which is interesting) to embed images with in outgoing email content.

Now to start with demo, say we have to send an email with Health of Weekly Sales Card for an online shopping store every week on Monday.

This email would look like the one in the following screen shot:

1

In Step 1,2,4 we are defining HTML that will be send as Email Content.

Here it is very important to notice the “SRC” Tag where we are specifying image path not as actual path but as a Content Identifier that is mapped to the path of the attachment added to the Email.

And this is the trick that you have to remember to get the images embedded into the html body of the email.

234

Now once we are done with defining body content for Email Body, we can start configuring SMTP Client and adding the attachments to the email

In Step 5 we will initialize “SMTP Server”, “Mail Message”, and “SMTP Client” Objects

In Step 6 configure “From” & “To” Email IDs for the intended recipients. Also specify the “Subject” & “Body” for the email

5

In Step 7 & 8 we will add attachments to the Email and provide each attachment a Content Identifier (remember we used this identifier in “SRC” Tag while defining HTML content for the body)

Attachments can be added to the email by using object of “Attachment” Class.

While initializing the “Attachment” Object we have to provide actual path of the attachment as input parameter

Then we have to make sure that

  1. “Inline” property should be set to “True”,
  2. “DispositionType” property should be set to “Inline”,
  3. “MediaType” property should be set to “<type of content, you are attaching>”,
  4. “ContentId” property should be set to any unique Content Identifier to represent the attachment uniquely

67

In Step 9 we will send the email by call in “Send” method of SMTP Client object. Make sure to dispose the “Attachments” & “Mail Message” objects

8

And finally we will call the Send-Email Function in Step 10 to run all the code snippets described earlier

9

That is all for this demo.

Hope you find it helpful.

 

SharePoint 2016: How To Implement Alternate Access Mapping

In this article we will discuss implementing “Alternate Access Mapping” or commonly known as “AAM” in SharePoint 2016.

Alternate Access Mapping Architecture

If you are not aware of AAM or you have some misconceptions about it, I would highly recommend you to read through an excellent blog Alternate Access Mappings (AAMs) *Explained by “Brain Pendergrass” from Microsoft and would like to thank him for such an awesome blog to make me understand this concept in depth.

This article will focus on guided steps to configure AAM in SharePoint 2016 and we won’t discuss AAM as a concept.

Create New Web Application

Step 1: To start the demo let go to SharePoint Central Admin Site and click on “Manage Web Applications” as shown below-

1

I am creating a new Web Application for demo purpose. In real environments we can use any existing Web Application to perform these steps.

Step 2: Click “New” menu to launch “Create New Web Application” wizard

2

Step 3: Enter Web Application Name and other necessary information

3

Once done click OK to start the process

4.0

When the creation process has been completed then we can see a new Web Application listed in the list of Web Applications

4.1

Also we may see the Modal Dialog that offers quick link to create new Site Collection for this Web Application

4.2

Create New Site Collection

Step 4: Click on “Create Site Collection” link to create new Site Collection for the Web Application.

Select Web Application

5

Enter Title, URLs, Template, Primary Site Collection Administrator

6

Click “OK” to start the process

7

Once process has been completed we can see new Site Collection created for the Web Application

9

We can navigate to the site collection by clicking the URL

10

Add DNS Entries

Step 5:  Now we have configure add host entries to DNS.

Search for DNS

11

Add new Host Entry to the “Forward Lookup Zone” as shown below-

12

In New Host screen, enter Host Name, Select FQDN, enter IP Address. Once done click “Add Host”

13

If operation completed successfully we can see popup window show success message

14

We can see this new Host added to the existing list

15

Add/Update IIS Bindings

Step 6: Add “IIS” Bindings

Now we have to add IIS bindings for the new Host. Search for “IIS” and select “Internet Information Services (IIS) Manager”

16

Select the Web Application that we have created in above steps from the “Connections” Panel on the left under “Sites” node

Click on “Bindings…” on the right to Add/Edit Web Application Bindings

17

In the “Site Bindings” screen select the default binding and click “Edit”

18

In the “Edit Site Binding” screen enter Host Name value and this should match the entry that we have created earlier in DNS

Click OK

19

Once saved the existing binding will look like as shown below-

20

Configure Alternate Access Mappings Using Central Admin

Step 7: Configure Alternate Access Mappings (From Central Admin)

Go To Central Admin -> “Application Management”

Under Web Applications Click “Configure alternate access mappings”

21

Click “Edit Public URLs”

22

In the “Edit Public Zone URLs” Screen and enter “Default Zone” URL with “Host Name” configured earlier as shown below.

Click “Save” once you entered the default Zone URL to save the data.

23

Now try accessing SharePoint Web Application using Public Zone URL and if the configuration goes well, the access will be granted to you

Test Alternate Access Mappings

2425

Configure Another Mapping

Step 8: Repeat Step 5 to add another DNS entry to add a new Host Name

262728

Step 9: Repeat Step 6 to Add Web Application Bindings

2930313233

Configure Alternate Access Mappings Using PowerShell

Step 10: Configure Alternate Access Mappings (Using PowerShell)

Now we will add this new Host Name to the default zone for the web application

Launch SharePoint 2016 Management Shell

34

“New-SPAlternateURL” cmdlet gives us option to add new Alternate URLs to the required zone for web application

This cmdlet take following parameters

  • New Alternate URL – URL that you need to register as alternate URL
  • Web Application Path – URL that represent Web Application
  • Zone – Represents a zone that you need this alternate URL to add to

35.0

Once this command executes successfully we can a new Alternate Access Mapping added to the list

35.1

Test Alternate Access Mapping

Now if try to access this web application using this new mapping it still be translated to the same Public URL for the Web Application.

3637

By following above steps we can enable a web application that receives the request from an internal URL in one of the five authenticated zones to return pages that contain links to the public URL for the zone

Hope you find it helpful.

SharePoint 2016: How To Get SPUser By User ID

While working with a whole range of applications using SharePoint Platform & Services I have come across lot of issues specially while dealing with SharePoint User Information let it by user names entered by application users on the UI or by querying user information by User ID.

In most scenarios you will need this to query data for the users based on User ID from within code itself. I have add a little User Interface in this article to make sure you get the idea on how this approach will work and what all properties will be exposed as results.

Here is the process flow that depicts the flow of information between Client Request & Server Response:

0

To start with the demo I have add some of HTML elements to prepare the UI with a textbox to enter User ID as shown below-

1

In the below screen shot you can see the simple HTML markup for the user interface.

We have a textbox where users can enter user id of the SharePoint User

2

In order to display the results I have add a HTML table as container. Purpose is to prepare HTML on the fly and paste it at runtime into this container.

3

In Step 1 we have bound the blur event of the textbox to a function that will execute the query against the User Data based on the User ID

4

In Step 2 we call another helper function “getUserById” by passing user id to it. This function call return a jQuery promise which can be further evaluated in upcoming steps

In Step 3 we will check if the JQuery call has been completed or not by using JQuery “when” construct.

In Step 4 we will call another helper function “renderUser” once the JQuery call has been completed in Step 3. The “renderUser” function is responsible to render User Information into the container.

5

In Step 5 we call “_api/Web” REST API endpoint can using its function “getUserById”, during this call we will specify “json” as datatype to ensure that we will get results in “json” format

6

In Step 6 we are rendering the details of the user in the container

7

And here is the final output of the operation performed.

So we can see Title, Login Name, Email returned back for a specific User Id as shown below-

8

That is all for this demo.

Hope you find it helpful.

 

SharePoint 2016: How to configure Usage & Health Service Application using PowerShell

Usage & Health Service Application is one of the most important Service Applications that provides vital information on the Health & State of SharePoint Farm.

In SharePoint 2016, Usage Service is collecting information on Timer Service Monitoring, Event Logs, Performance Counters, Search Usage, Sandbox Usage, and Site Collection Usage and much more.

This Service application is responsible to keep monitoring the resource, health & state associated with the SharePoint Farm and logs this information to Log Files on SharePoint Hive or any designated path along with SharePoint Logging Database “WSS_Logging” in SQL Server Database.

Many other components in SharePoint like Developer Dashboard, Search Analytics, Web Analytics Reports are using this data to provide logical User Interface for the end users.

You will get empty reports in Web Analytics reports section, if usage and health data collection service application is not configured properly.

It is not possible to Provision Usage & Health Service using Central Admin, as there is no User Interface available.

Let us first get into Central Admin Site to see if there is options available on the User Interface to provision Usage Service

Go to “Central Admin” – > “Manage Service Applications”

2

Click on “New” Menu on the Ribbon and in the existing list of available Service Application Templates we can see there is no template available for provisioning “Usage & Health Service Application”

3

This concludes that we have to provision Usage Service using PowerShell as shown in the following steps-

1

Launch “SharePoint 2016 Management Shell”

4

Step 1: Provision Service Application Instance

In this step, we will provision the Usage Service using “New- SPUsageApplication” cmdlet as shown below

New-SPUsageApplication -Name "Usage and Health Data Collection"

5

Step 2: Provision Service Application Proxy

In this step, we will provision Service Application Proxy for Usage Service by using the following commands

$serviceProxy = Get-SPServiceApplicationProxy | where {$_.TypeName -eq "Usage and Health Data Collection Proxy"}

6

$serviceProxy.Provision()

7

Step 3: Verify Service Application

In this step we will verify the provisioning of service application by looking at “Service Application” Page in “Central Administration” as shown below-

8

Step 4: Verify Service Application Database

In this step we will verify the provisioning of service application database by logging into “SQL Server Management Studio” as shown below-

9

10

This concludes the Service Application Provisioning Process using PowerShell.

Hope you find it helpful.