Showing posts with label spsite. Show all posts
Showing posts with label spsite. Show all posts

June 18, 2009

How to create create a new SharePoint site programmatically

This article shows how to create a new SharePoint site programmatically. In this example, we will create a sub-site of the top level site (root) of a site collection.

To add a sub-site to site, we have to use the SPWeb.Webs.Add method which creates and returns a new SPWeb object. See this MSDN link for more information about the different parameters of the method:
http://msdn.microsoft.com/en-us/library/ms473439.aspx

One time the site is created, we have to create the 3 default groups (Owners, Members and Visitors) and associate them with the new site. For that, we have to use a selfmade class called SPGroupsHelper which creates a group and its associations with the site.

Hereunder is a little console application showing a to create a site programmatically using unique permissions and based on the team site template:


using System;

using Microsoft.SharePoint;

using SiteCreation.Helpers;

 

namespace SiteCreation

{

    class Program

    {

        static void Main(string[] args)

        {

            using (SPSite site = new SPSite("http://mysitecollectionurl/"))

            {

                using (SPWeb web = site.OpenWeb(site.RootWeb.ID))

                {

                    try

                    {

                        web.AllowUnsafeUpdates = true;

 

                        string url = "TESTCREATION";

                        string title = "Test Creation";

                        string description = "This is a test site";

                        string type = "STS#0";

 

                        // Site creation with unique permissions

                        SPWebCollection webs = web.Webs;

                        SPWeb newWeb = webs.Add(url, title, description, 1033, type, true, false);

 

                        // Owners, members and visitors groups creation

                        SPGroup owners = SPGroupsHelper.AddGroup(newWeb, SPGroupsHelper.AssociatedGroupTypeEnum.Owners);

                        SPGroup members = SPGroupsHelper.AddGroup(newWeb, SPGroupsHelper.AssociatedGroupTypeEnum.Members);

                        SPGroup visitors = SPGroupsHelper.AddGroup(newWeb, SPGroupsHelper.AssociatedGroupTypeEnum.Visitors);

 

                        // Changing the request access email to current user

                        newWeb.RequestAccessEmail = newWeb.CurrentUser.Email;

 

                        // Save changes

                        newWeb.Update();

 

                        // Disposing new web object

                        newWeb.Dispose();

                    }

                    catch (Exception ex)

                    {

                        Console.WriteLine(ex.Message);

                    }

                    finally

                    {

                        web.AllowUnsafeUpdates = false;

                    }

                }

            }

            Console.ReadLine();

        }

    }

}


Hereunder is the selfmade SPGroupsHelper classe used in the little console application:

using Microsoft.SharePoint;

 

namespace SiteCreation.Helpers

{

    public static class SPGroupsHelper

    {

 

        public enum AssociatedGroupTypeEnum

        {

            Owners,

            Members,

            Visitors

        };

 

        public static SPGroup AddGroup(SPWeb web, AssociatedGroupTypeEnum associateGroupType)

        {

            switch (associateGroupType)

            {

                case AssociatedGroupTypeEnum.Owners:

                    return AddGroup(web, "{0} Owners", "Use this group to give people full control permissions to the SharePoint site: {0}", SPRoleType.Administrator, "{0} Owners");

                case AssociatedGroupTypeEnum.Members:

                    return AddGroup(web, "{0} Members", "Use this group to give people contribute permissions to the SharePoint site: {0}", SPRoleType.Contributor,"{0} Owners");

                case AssociatedGroupTypeEnum.Visitors:

                    return AddGroup(web, "{0} Vistors", "Use this group to give people read permissions to the SharePoint site: {0}", SPRoleType.Reader,"{0} Owners");

                default:

                    return null;

            }

        }

 

        public static SPGroup AddGroup(SPWeb web, string groupNameFormatString, string descriptionFormatString, SPRoleType roleType, string ownerNameFormatString)

        {

            web.SiteGroups.Add(string.Format(groupNameFormatString, web.Title), web.CurrentUser, web.CurrentUser, string.Format(descriptionFormatString, web.Name));

 

            SPGroup group = web.SiteGroups[string.Format(groupNameFormatString,web.Title)];

            try

            {

                SPGroup owner = web.SiteGroups[string.Format(ownerNameFormatString, web.Title)];

                group.Owner = owner;

            }

            catch { }

 

            if (descriptionFormatString.IndexOf("{0}") != -1)

            {

                SPListItem item = web.SiteUserInfoList.GetItemById(group.ID);

                item["Notes"] = string.Format(descriptionFormatString, string.Format("<a href=\"{0}\">{1}</a>", web.Url, web.Name));

                item.Update();

            }

 

            SPRoleAssignment roleAssignment = new SPRoleAssignment(group);

            roleAssignment.RoleDefinitionBindings.Add(web.RoleDefinitions.GetByType(roleType));

            web.RoleAssignments.Add(roleAssignment);

            switch(roleType)

            {

                case SPRoleType.Administrator:

                    group.AllowMembersEditMembership = false;

                    group.OnlyAllowMembersViewMembership = true;

                    group.AllowRequestToJoinLeave = false;

                    group.AutoAcceptRequestToJoinLeave = false;

                    web.AssociatedOwnerGroup = group;

 

                    break;

                case SPRoleType.Contributor:

                    group.AllowMembersEditMembership = false;

                    group.AllowRequestToJoinLeave = false;

                    group.AutoAcceptRequestToJoinLeave = false;

                    group.OnlyAllowMembersViewMembership = false;

                    web.AssociatedMemberGroup = group;

                    break;

                case SPRoleType.Reader:

                    group.AllowMembersEditMembership = false;

                    group.OnlyAllowMembersViewMembership = true;

                    group.AllowRequestToJoinLeave = false;

                    group.AutoAcceptRequestToJoinLeave = false;

                    web.AssociatedVisitorGroup = group;

                    break;

            }

            group.Update();

            web.Update();

            return group;

        }

    }

}

December 13, 2008

SharePoint Used Space Information

SharePoint Used Space Information is a console application which allows to retrieve information about space used (bytes) by all sites and sub-sites for a specific site collection into .csv files.

This tool uses the StorageManagementInformation method of the SPSite class to retrieve the usage information.

You can find the solution on CodePlex: http://www.codeplex.com/SPUsedSpaceInfo

September 11, 2008

How to get usage information for a specific site collection by code

To get usage information for a site collection, have a look at the Usage property of the SPSite class.

This property is a structure which contains 5 fields:
- Storage: total amount of storage (bytes) consumed
- DiscussionStorage: amount of storage (bytes) used by the web discussion data
- Hits: cumalative number of visits
- Bandwidth: cumulative bandwith used
- Visits: cumulative number of visits

Note that 2 (Storage and DiscussionStorage) are updated in real time and 3 (Bandwidth, Hits and Visits) are updated daily by the usage analysis timer job.

The following console application displays the usage information for a specific site collection:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
 
namespace UsageInformation
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite siteCollection = new SPSite("http://siteCollectionUrl"))
            {
                Console.WriteLine(string.Format("SiteCollection Url: {0}", siteCollection.Url));
                Console.WriteLine(string.Format("Storage: {0}", siteCollection.Usage.Storage));
                Console.WriteLine(string.Format("Discussion Storage: {0}", siteCollection.Usage.DiscussionStorage));
                Console.WriteLine(string.Format("Bandwidth: {0}", siteCollection.Usage.Bandwidth));
                Console.WriteLine(string.Format("Hits: {0}", siteCollection.Usage.Hits));
                Console.WriteLine(string.Format("Visits: {0}", siteCollection.Usage.Visits));
 
                Console.WriteLine(string.Format("Press a touch to exit..."));
                Console.ReadLine();
            }
        }
    }
}

August 19, 2008

SPSite Storage Information

SPSite Storage Information provides storage reports about documents, documents libraries and lists for a specific site collection in SharePoint. The reports can be exported in excel or pdf.

This tool uses the StorageManagementInformation method of the SPSite class to retrieve the storage information.

You can find the solution on CodePlex:
http://www.codeplex.com/SPSiteStorageInfo