SharePoint 2013: How To Create A New Site Collection

To create a new site collection on SharePoint 2013 server, open Central Administration and under the Application Management section select Create site collections:

SP2013 Create Site Collection 1

Enter a Title and Description for your site, so for the title I am using Demo Site 1. Web Site Address you may select an alternative URL Path or use the default URL Path sites and then enter your URL name, for this demonstration I will use DemoSite1.

SP2013 Create Site Collection 2

(more…)

SharePoint: How to Change The Default Welcome Page on a Site

If you want to programmatically set a site’s default welcome page you can write the following code to redirect it to another page within the site.

The url path must relative and you MUST create another SPFolder object to reference the RootFolder and perform your update, or else it won’t work:

using Microsoft.SharePoint;

namespace SharePointConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var spSite = new SPSite("http://spdev/test"))
            {
                using (var spWeb = spSite.OpenWeb())
                {
                    var rootFolder = spWeb.RootFolder;
                    rootFolder.WelcomePage = "Lists/Events/AllItems.aspx";
                    rootFolder.Update();
                }
            }
        }
    }
}

SharePoint 2010: Event Receiver ItemAdding and ItemUpdating How To Set AfterProperties DateTime Field Value

If you are receiving this error “Invalid date/time value. A date/time field contains invalid data. Please check the value and try again.”. This is because the assigning value for the AfterProperties DateTime field in the ItemAdding or ItemUpdating event in the Event Receiver was not formatted correctly.

To resolve this issue you need to format the DateTime value to an ISO8601 string format:

properties.AfterProperties["StartDate"] = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ssZ");

or by using the SPUtility.CreateISO8601DateTimeFromSystemDateTime method:

properties.AfterProperties["StartDate"] = Microsoft.SharePoint.Utilities.SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Now);

 

SharePoint 2010: Add, Update and Delete List Items Using The Server Side Object Model

In this blog post I will explain and demonstrate how to Add, Update and Delete a SharePoint List Item using the server side object model.

Adding a New Item
In the code sample below I obtain the current context and the web and get the list I would like to add the new item to. After I have assigned values to my fields I call an update method from the item.

SPWeb spWeb = SPContext.Current.Web;
SPList spList = spWeb.Lists["MyList"];
SPListItem item = spList.Items.Add();
item["Title"] = "Hello, world!";
item.Update();

 
Update an Existing Item

Updating an existing item from a SharePoint List is very similar to adding a new item, but the only difference is instead of calling Add() from the SPListItemCollection, I now call the GetItemById(int) from the SPList object. There are other ways of getting an item from a list and another one is GetItemByTitle.

SPWeb spWeb = SPContext.Current.Web;
SPList spList = spWeb.Lists["MyList"];
SPListItem item = spList.GetItemById(1);
item["Title"] = "Hello, world!";
item.Update();

 
Deleting an Existing Item
Deleting an existing item from a SharePoint List all I need to do is get the item I want from this list and then call the Delete method from the item object. That’s how easy it is.

SPWeb spWeb = SPContext.Current.Web;
SPList spList = spWeb.Lists["MyList"];
SPListItem item = spList.GetItemById(1);
item.Delete();

I hope the information I have provided in this post is useful to you.

SPGridView: How to Auto Generate Columns

I have been working on a project recently that require the use of the SPGridView web control and realise that it doesn’t auto generate columns for you, even if you set the property to true it will complain. So to overcome this issue I had to write a some code to achieve what I want it to do.

First I need to use the BoundField for setting up the column and binding to a SharePoint List and there is two properties that I need to set, which is the HeaderText for the column display text and DataField for binding to the list’s field.

To set these two properties I need to instantiate a SPList and get the list’s SPView object that I need, when I have the SPView object I can iterate through the ViewFields collection to get each of the field internal names for setting the DataField property and to get the display name I had to take the current list’s SPField collection and use a method called GetFieldByInternalName to return a SPField. Then I use the SPField Title property to set the HeaderText property.

private void GenerateColumns(SPGridView spGridView, string listName, string viewName)
{
    var spWeb = SPContext.Current.Web;
    var spList = spWeb.Lists[listName];
    var spView = spList.Views[viewName];
    var spFields = spList.Fields;

    foreach (string intNameField in spView.ViewFields)
    {
        var spField = spFields.GetFieldByInternalName(intNameField);
        if (spField != null)
        {
            var boundField = new BoundField
                {
                    HeaderText = spField.Title,
                    DataField = intNameField
                };
            spGridView.Columns.Add(boundField);
        }
    }
}

Now I have the created method to auto generate the columns I need to make sure the datasource I am binding to the SPGridView control contains the same internal field name of the list or else the code above won’t work.

SharePoint: Hierachy and the Object Model

SharePoint Hierarchy

I writing this post to let others understand the basics of the SharePoint hierarchy and the Object Model. I know as a developer it can get confusing with the terminologies. This post I would like to address the main areas of the hierarchy and explain their purposes: 

Farm:

The farm is a collection of

  • front end web servers – these are the web site or also known as web applications
  • application servers – these are the servers that provide the back end services such as searching, office services and etc.
  • databases servers – these are the servers that stores the configuration content of the whole farm.

In the SharePoint Object Model the SPFarm class is the first tier of the hierarchy, this class consist of two properties:

  • Servers – this property gets all the servers in the farm in a collection of SPServer objects.
  • Services – this property gets all the services in the farm in a collection of SPService objects. (more…)
Shares