Jean Paul's Blog

There are 2 types of People in the World, One who Likes SharePoint and..

    Advertisements
  • Microsoft MVP

  • MindCracker MVP

  • CodeProject MVP

  • eBook on SharePoint 2010

  • eBook on Design Patterns

  • eBook on Windows Azure

  • NLayers Framework @ CodePlex

  • MSDN Forums

  • .Net vs. Java

    Due to Public Demand
  • Advertisements

Archive for October, 2014

SharePoint & Http Module Example

Posted by Paul on October 30, 2014

In this article I would like to take you through deploying Http Module in SharePoint 2013.

Scenario

Redirect users from accessing the Site Contents page (viewlsts.aspx)

Steps

Open Visual Studio and create a Class Library project.

image

Add reference to System.Web library.

image

Replace the class file with following code where:

  • Class implements the IHttpModule interface
  • URL redirection for viewlsts.aspx to www.bing.com

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.UI;

namespace SPModule
{
    public class SPModule : IHttpModule
    {
        public void Dispose()
        {
        }

        public void Init(HttpApplication context)
        {
            context.PreRequestHandlerExecute += context_PreRequestHandlerExecute;
        }

        void context_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            HttpApplication app = sender as HttpApplication;

            if (app != null)
            {
                Page page = app.Context.CurrentHandler as Page;

                if (page != null)
                {
                    if (HttpContext.Current.Request.Url.AbsoluteUri.Contains(“_layouts/15/viewlsts.aspx”))
                    {
                        HttpContext.Current.Response.Redirect(“http://www.bing.com”);
                    }
                }
            }
        }
    }
}

Build the code & Copy the DLL to IIS SharePoint BIN folder as shown below.

image

Now open the web.config and find the modules section.  (Please note that there is another httpModules section which is not for our purpose)

image

Add your class name in the format Assembly.ClassName.  Save changes.

Refresh your SharePoint site and try accessing the viewlsts.aspx page using URL given below.

http://server/_layouts/15/viewlsts.aspx

You should see the page is redirected to www.bing.com.

image

References

http://msdn.microsoft.com/en-us/library/office/bb862635(v=office.12).aspx

Summary

In this article we have seen how to use HTTP Module within SharePoint.

Advertisements

Posted in SharePoint, SharePoint 2013 | Tagged: , | Leave a Comment »

Create Content Type using Object Model

Posted by Paul on October 30, 2014

In this article we can try creating a Content Type using Server Object Model code.

Steps

Please follow the steps below to create the content type.

Create a Console Application & Add Reference to Microsoft.SharePoint.dll. Also make sure the project properties > Build tab > Prefer 32-bit is unchecked.

image

Use the following code for the console application.

using (SPSite site = new SPSite(“http://hpvm”))

{

using (SPWeb web = site.OpenWeb())

{

// Delete content type if already existss

if (web.ContentTypes[“CodeCT”] != null)

web.ContentTypes[“CodeCT”].Delete();

// Create content type inheritin from ‘Item’

SPContentType contentType = new SPContentType(web.ContentTypes[“Item”], web.ContentTypes, “CodeCT”);

web.ContentTypes.Add(contentType);

contentType.Group = “Custom Content Types”;

contentType.Description = “Content Type created through Code”;

// Create Site Columns

string fieldName = web.Fields.Add(“Column 1”, SPFieldType.Text, false);

SPField field = web.Fields.GetFieldByInternalName(fieldName);

field.Update();

fieldName = web.Fields.Add(“Column 2”, SPFieldType.Number, false);

field = web.Fields.GetFieldByInternalName(fieldName);

field.Update();

fieldName = web.Fields.Add(“Column 3”, SPFieldType.DateTime, false);

field = web.Fields.GetFieldByInternalName(fieldName);

field.Update();

// Add Field References to Our Site Columns

contentType.FieldLinks.Add(new SPFieldLink(web.Fields.GetField(“Column 1”)));

contentType.FieldLinks.Add(new SPFieldLink(web.Fields.GetField(“Column 2”)));

contentType.FieldLinks.Add(new SPFieldLink(web.Fields.GetField(“Column 3”)));

// Add few fields from OOB Site Columns

contentType.FieldLinks.Add(new SPFieldLink(web.Fields.GetField(“_Status”)));

// Update

contentType.Update();

}

}

Console.WriteLine(“Content Type created successfully.”);

}

The code is executed below.

1. Delete any content type with name CodeCT

2. Create new content type

3. Add custom field definitions to the web

4. Add custom field references to the content type

5. Add OOB column _Status to the content type

6. Updates content type

image

For custom columns, we are actually creating a Site Column through code (Fields.Add) & Referring it to the content type (FieldLinks.Add).

Execute the code.

image

You can verify the new content type being created or not from Site Settings > Site content types.

image

Click on it & ensure the all columns are visible.

image

You can use the SPFieldType enum for finding more types.

References

http://msdn.microsoft.com/en-us/library/ff798370.aspx

Summary

In this article we have explored how to create content type using server object model code. The source code is attached for reference.

Posted in SharePoint, SharePoint 2013 | Tagged: , , , | Leave a Comment »

Create Content Type using Schema

Posted by Paul on October 29, 2014

In this article we can explore how to create a content type using XML Schema.

Activities Summary

Following are our activities:

1. Create Project in Visual Studio

2. Define Content Type Fields

3. Deploy Solution

Steps

Create a new project in Visual Studio. Choose Farm Solution. Add New Item > Content Type into it from the Office/SharePoint category of templates.

image

Inherit from Item as parent.

image

Close the designer view of content type & use Solution Explorer to open the XML view.

image

You will get the following view.

image

Replace the content with following.

<?xml version=”1.0″ encoding=”utf-8″?>

<Elements xmlns=”http://schemas.microsoft.com/sharepoint/”&gt;

<Field ID=”{00D74D75-B775-4222-967D-D26973B8C5DB}” Name=”CustomField” DisplayName=”Custom Field” Type=”Text”/>

<Field ID=”{C3CD561C-D714-42DE-8112-C830B4108CEF}” Name=”CustomChoiceField” DisplayName=”Custom Choice Field” Type=”Choice”>

<CHOICES>

<CHOICE>Choice 1</CHOICE>

<CHOICE>Choice 2</CHOICE>

<CHOICE>Choice 3</CHOICE>

</CHOICES>

</Field>

<!– Parent ContentType: Item (0x01) –>

<ContentType ID=”0x0100CB3ADB9F2D014E3F920C47C0B14D25BD” Name=”SchemaCT” Group=”Custom Content Types” Description=”My Content Type” Inherits=”TRUE” Version=”0″>

<FieldRefs>

<FieldRef ID=”{00D74D75-B775-4222-967D-D26973B8C5DB}” Name=”Custom Field” DisplayName=”Custom Field”/>

<FieldRef ID=”{C3CD561C-D714-42DE-8112-C830B4108CEF}” Name=”Custom Choice Field” DisplayName=”Custom Choice Field”/>

<FieldRef ID=”{1DAB9B48-2D1A-47b3-878C-8E84F0D211BA}” DisplayName=”$Resources:core,Status;” Name=”_Status”/>

</FieldRefs>

</ContentType>

</Elements>

Let us try to understand what each XML block do:

· The Elements section specifies the XML namespace url. We can include Field definitions here. Fields are first defined & later referenced.

· The first Field is of type Text. We need to specify GUID for each Field

· The second Field is of type Choice. The Choice values are specified in child tag

· The section Content Type specifies information on content type like Name, Description, Group, Version etc.

· The FieldRefs section is important as it actually specified the Fields used in the content type. Please note that we are referring the Fields here which we defined previously.

· Each Field Reference should include the ID, Name and Display Name of it. We can also refer existing Site Columns here by using the ID. We are using 2 custom fields & 1 existing site column here.

Now save changes & Deploy the project. Once the deployment succeeded, open SharePoint to view the new content type in Site Settings > Site content types page.

image

You can see the columns being showing up there.

image

This concludes our content type creation using XML Schema. If you wanted to create different field types, please see the field types in references section.

References

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfieldtype.aspx

Summary

In this article we have explored how to create a content type using XML Schema.

Posted in SharePoint, SharePoint 2013 | Tagged: , , , | Leave a Comment »

Create Content Type Hub in SharePoint 2013

Posted by Paul on October 25, 2014

In this article we can explore how to create Content Type hub in SharePoint 2013.

Content Type Hub

Content Type Hub is a centralized location where we manage, publish content types to other web applications.

· Content Type Hub is actually a Site Collection.

· Content Type is published & subscribed using the Managed Metadata Service application.

Steps

Follow the steps below to create a content type hub. Open Central Administration > Manage Web Applications > Create New Web Application. This will be our publishing application.

image

Create a new site collection under the new web application.

image

The site collection looks like below.

image

Create a new content type under the site collection. Name it as Travel Request.

image

Open Central Administration > Manage Service Application > Choose Managed Metadata Service > Click Properties.

image

Enter the Content Type Hub URL as shown below.

image

Now choose the connection (sub item) of Managed Metadata Service

image

Check the following options which ensure subscribing published content types.

image

Back to our Source Content Type > Choose the Content Type Settings > Manage Publishing for this Content Type.

image

Choose the Publish option and click the OK button.

image

Go to Central Administration > Monitoring > Review Job Definitions. Select the following jobs:

1. Content Type Hub

2. Content Type Subscriber (For Default SharePoint Site)

image

Choose the Run Now option for each job as shown below inducing few minutes delay in between.

image

Refresh the Subscribing site collection & you should be able to see the new content type.

image

This concludes our content type publishing & subscribing.

Summary of Activities

Following were the summary of activities:

1. Create Content Type Hub – Web Application & Site Collection

2. Create Content Type under Site Collection

3. Manage Metadata Service Application

4. Manage Metadata Service Application Connection

5. Publish Content Type

6. Run Timer Jobs

7. Refresh Subscribing Site Collection

8. Ensure Content Type is visible

References

http://bit.ly/1EQ4RqX

Summary

In this article we have explored how to create content type hub.

Posted in SharePoint, SharePoint 2013 | Tagged: , , | Leave a Comment »

Hide Wiki Page Layouts

Posted by Paul on October 23, 2014

In this post I will show how to hide Wiki Page layouts.  Following are the page layouts for a wiki while the Publishing feature is enabled.

image

But, some of the clients feels difficult to operate.  They wanted to minimize the number of available layouts.

Scenario

To create the scenario please follow the steps below.  Create a Publishing Site & Choose Add a Page option.

image

In the page layout tab you will be able to see multiple layouts.

image

How to hide these?

Open the Site Settings page.

image

Scroll to the middle & choose the third radio button.  Ensure you are only selecting the required page layout.

image

Save changes.  Alternatively, you can choose the Reset all option.

Come back to the site and try adding a new page.  You should see only one layout now.

image

Summary

In this post we have explored how to hide wiki page layouts.

Posted in SharePoint, SharePoint 2013 | Tagged: , , | Leave a Comment »

Programmatically Read Discussion Board & Replies

Posted by Paul on October 21, 2014

I searched a lot for Server Object Code in reading Discussion Board Items & Replies. As cannot find one, following is created.

Challenge

You need to programmatically get all discussion items along with their replies.

Steps

This is our Discussion Board.

image

This is the code to retrieve all discussion items.

using (SPSite site = new SPSite(“http://hpvm&#8221;))

{

using (SPWeb web = site.OpenWeb())

{

SPList list = web.Lists.TryGetList(“Discussion Board”);

if (list != null)

{

SPQuery query = new SPQuery();

query.ViewFields = “<FieldRef Name=’Title’ />”;

SPListItemCollection itemColl = list.GetItems(query);

foreach (SPListItem item in itemColl)

{

System.Console.WriteLine(item[“Title”].ToString());

}

}

}

}

Make sure the code is returning values like this.

image

Now we are writing the code to return the replies.

private static void GetReplies(SPList list, SPListItem item)

{

SPQuery query = new SPQuery();

query.Folder = item.Folder;

SPListItemCollection collection = list.GetItems(query);

SPListItem replyItem = collection[0];

string body = (string)replyItem[“Body”];

string author = (string)replyItem[“Author”];

System.Console.WriteLine(body);

System.Console.WriteLine(author);

}

Following is the output on running the console application.

image

You can see the Author is returned mixing with ID. You can use the following method to extract the correct User Name from the string.

private static string GetUserName(string author, SPList list)

{

string result = author;

if (author.Contains(“;”))

{

int id = int.Parse(author.Split(‘;’)[0]);

SPUser user = list.ParentWeb.SiteUsers.GetByID(id);

result = user.Name;

}

return result;

}

Now the user name seems to be better.

image

image

I am creating a Console Application to retrieve the same.

References

http://bit.ly/1tIX5NV

Summary

In this article we have explored how to fetch Discussion Board Items & Replies. The source code is attached here with the article.

Posted in SharePoint, SharePoint 2013 | Tagged: , , , , | Leave a Comment »

Add User to Azure AD

Posted by Paul on October 20, 2014

In this article we can see how to add a user to the Azure Active Directory.

Open Management Portal

Open Azure Management Portal and click on the Active Directory pane.

image

Click on the Default Directory instance and then on the USERS tab.  You will see the following page.

image

Click on the ADD USER button from the bottom pane.

Enter information about the user.

image

Choose the Role as User.

image

Click the Create button for temporary password generation.

image

image

You can see the new user being created as shown below.

image

image

The user should also get email in the specified email address.

References

http://msdn.microsoft.com/en-us/library/dn414569(v=nav.80).aspx

Summary

In this article we have explored how to add a user to Azure AD.

Posted in Uncategorized | Leave a Comment »

Create Azure Account

Posted by Paul on October 15, 2014

In this article we can see how to Create Azure Account. I am going to use a Trial one.

Trial

You can create a Trial Account using the link below.

http://azure.microsoft.com/en-us/pricing/free-trial/

image

Click on the Try it now button. In the appearing page, enter your Microsoft Account.

image

Proceed with the Free Trial option. You need to verify with your phone number as well.

image

You can enter your Credit Card information to Purchase the trial.

image

image

Please note that once the trial period exceeds, the credit card will get charged. Make sure you close your trial before the credit amount or time expires.

You might need to wait for few hours for the trial account to be created. My account is shown below accessed by URL: https://manage.windowsazure.comimage

You can always check the Credit Status by clicking the Microsoft Azure text in left-top corner.

image

Now you are ready to experiment with Azure Account.

References

http://azure.microsoft.com/en-us/documentation/

Summary

In this article we have explored how to Create Azure Account.

Posted in Uncategorized | Leave a Comment »

Create Site Column

Posted by Paul on October 12, 2014

In this article we can explore how to create a site column in SharePoint 2013.

Site Column

Following are the properties of a site column:

1. Site Column is a Type

2. Site Column allows Reusability

3. Site Column can be used in Content Types, Lists, Libraries

4. Site Columns can be OOB & Custom

5. Site Column can be created through SharePoint user interface & Programmatically

View Site Columns

You can view the site columns from Site Settings > Site Columns link.

image

You can see various site columns having different data types.

image

Create Site Column

Click on the Create button to create a new site column. You will get the following page.

image

You can specify the following:

1. Name of the site column

2. Type of the site column

3. Group of the site column

4. Description of the site column

5. Other custom properties

In the above example, I am creating a site column named SSN which is of data type Number. Click Ok button to create the site column.

You can see the new site column in the list.

image

Now you are ready to reuse the site column in your content type, list or library.

References

http://msdn.microsoft.com/en-us/library/hh191454.aspx

Summary

In this article we have explored Site Columns and how to create it.

Posted in SharePoint, SharePoint 2013 | Tagged: , , , | Leave a Comment »

70-488 Module1 – Mange the Site Life Cycle

Posted by Paul on October 9, 2014

This is part of preparation-series for 70-488.

Topics

Following are the topics:

1. Create a delayed site collection

2. Manage site collection disposition

3. Identify inactive site collections

4. Backup and archive site collection data

Create a Delayed Site Collection

Delayed Site Collection is a Site Collection created but the Template will be chosen later. For creating a delayed site collection, open Central Administration > Create site collections > Custom tab as shown below.

image

Wait for the site collection to be created & Open it in a new window. You can see the site asks for template.

image

image

This is good for Administrators where a Site Collection request comes in without specifying the template.

Managing Site Collection Disposition

Site Collection Disposition deals with Closing & Deletion of site. Closing is the process of preventing site collection from access. Deletion removes the site collection.

We can create a Policy to achieve the same. Open Site Settings > Site Policies page.

image

You will get the following page.

image

Click the Create button to create a new policy.

image

In the appearing page enter the following:

1. Name for the policy

2. Close and Delete functionality

Save changes & your new policy is created.

image

In the above example, I have selected 1 year from creation date to close the site. Plus, 1 month for deletion from closing date. SharePoint internal timer jobs will be carrying out these tasks and appropriate email notifications will be sent.

image

It is a good habit to associate Disposition policy on site collection creation. This will ensure unused or irrelevant data being occupying content database & search results.

Identify Inactive Site Collections

To identify inactive site collections, we can use PowerShell script. We can use the SPSite.LastContentModifiedDate property to find obsolete site collections.

image

Following is the PowerShell script:

Add-PSSnapIn -Name Microsoft.SharePoint.PowerShell

$farm = [Microsoft.SharePoint.Administration.SPFarm]::Local

$websvcs = $farm.Services | where -FilterScript {$_.GetType() -eq [Microsoft.SharePoint.Administration.SPWebService]}

foreach ($websvc in $websvcs)

{

foreach ($webapp in $websvc.WebApplications)

{

foreach ($site in $webapp.Sites)

{

echo $site.HostName $site.Url $site.LastContentModifiedDate $site.ContentDatabase.Name $site.WebApplication.Name

}

}

}

The PowerShell script is available at:

http://www.sharepointcto.com/PowerShell/PowerShell.aspx

Backup and Archive Site Collection Data

Backing up a Site Collection is possible in following ways:

1. Backup through Central Administration

2. Backup through PowerShell

Following is the Backup through Central Administration.

image

Choose the Perform a site collection backup link item.

image

The backup process will be initiated in the background.

Archival is the process of storing data for historical purpose & preventing changing of content. Although SharePoint 2013 does not allow any direct-archival mechanisms, we can employ the following methods to achieve it.

1. Use Close policy

2. Move site collection to another location

image

Archival also ensures the content is not visible in any Search.

References

http://technet.microsoft.com/en-us/library/jj219569(v=office.15).aspx

Summary

In this article we have explored managing site life cycle.

Posted in SharePoint, SharePoint 2013 | Tagged: , , | Leave a Comment »