Monday, September 3, 2012

What is a Property Bag in sharepoint 2010


In this post we are going to discuss what is Property Bag in sharepoint 2010.

Basically this property bag is a feature available in Windows SharePoint Services 3.0.
It is a Hash Table where in we will maintain key-Value information. The basic fundu of this Property bag is to add the properties to the objects of the SharePoint site.

Why Property bag instead of web.config?
While defining the property bag we said it is an Hash Table where we will maintain Key-Value information. The question arises here is, when we can maintain key-value information in web.config(appsettings section-Ref below appsettings) why should we go for Property bag.

<configuration>
    <appSettings>
        <add key="MyKey" value="MyValue" />
    </appSettings>
</configuration>

And the answer is, When we add key-value in web.config which will become common to the entire web application.

What, if there is any setting specific to individual site in the web application?
In this scenario we will have to adopt either of the below cases.
è        1) Maintain multiple key-value entries in web.config.
è        2) Use sharepoint property bag. With this we can store the properties in several levels like SPFarm, SPWebApplication, SPSite, SPWeb and SPList.

Property Bag in sharepoint 2010:
There is no specific user interface available to add/modify the property bag settings. We have to do it through object model.
Also there is an option available in SharePoint designer to add/modify the property bag settings. Go to Site -> Site Settings. click on the Parameters tab.  On this tab, you will be able to manipulate all of your custom property bag values.

Add/Modify Property bag with Object model:
In the below example we are adding/reading/updating/deleting the properties at the farm level. Similarly we can do it for rest all the scopes mentioned above.

Adding:
SPFarm myFarm = SPFarm.Local;
myFarm.Properties.Add("myFarmKey", "myFarmValue");
myFarm.Update();

Reading:
SSPFarm myFarm = SPFarm.Local;
if (myFarm.Properties != null && myFarm.Properties.Count > 0)
{
if (myFarm.Properties.ContainsKey("myFarmKey"))
{
String myFarmValue= myWebApplication.Properties["myFarmKey"];
}
}

Updating:
SPFarm myFarm = SPFarm.Local;
myFarm.Properties["myFarmKey"] = "myNewFarmValue";
myFarm.Update();

Deleting:
SPFarm myFarm = SPFarm.Local;
myFarm.Properties["myFarmKey"] = null;
myFarm.Properties.Remove("myFarmKey");
myFarm.Update();


Hope this helps…

Wednesday, March 7, 2012

Programatically set the welcome page of a sharepoint site MOSS2010

In this post we will see how to change the welcome page of a sharepoint site using object modelling.

Normally for any sharepoint site, the welcome page will be default.aspx. In this post we will set the Home.aspx page(present in pages library) as the welcome page, so that home.aspx will open as soon as user opens the site.

Below is the code snippet::

        /// <summary>
        /// method to add welcome page
        /// </summary>
        /// <param name="publishingWeb"></param>
        /// <param name="oWeb"></param>
        /// <param name="pageName"></param>
        private void CreateWelcomePage(PublishingWeb publishingWeb, string pageName)
        {
            try
            {
                string fullPageUrl = publishingWeb.Url + "/Pages/" + pageName + ".aspx";
                SPFile fileNew = publishingWeb.Web.GetFile(fullPageUrl);
                publishingWeb.DefaultPage = fileNew;
                fileNew.Publish(pageName + ".aspx set as Welcome Page");
                fileNew.Approve(pageName + ".aspx set as Welcome Page");
                publishingWeb.Update();
            }
            catch (Exception ex)
            {
               
            }

        }

Call the above method as below
CreateWelcomePage(myPublishingWebObj,"Home");


Hope this helps...
Next post: Programatically applying master page in sharepoint

Programatically changing the page layout of default.aspx page MOSS2010

In this post we will change the page layout of default.aspx page MOSS2010 using object modelling.
Below is the code snippet::

        /// <summary>
        /// Method to Change PageLayout For DefalutPage
        /// </summary>
        /// <param name="publishingWeb"></param>
        /// <param name="currentWeb"></param>
        public void ChangePageLayoutForDefalutPage(PublishingWeb publishingWeb, string customPageLayout)
        {

            PublishingPage publishingPage = publishingWeb.GetPublishingPage(publishingWeb.Url + "/Pages/default.aspx");

            if (publishingPage != null)
            {
                List<PageLayout> layouts = new List<PageLayout>(publishingWeb.GetAvailablePageLayouts());
                PageLayout newPageLayout = layouts.Find(
                                delegate(PageLayout l)
                                {
                                    return l.Name.Equals(customPageLayout, StringComparison.CurrentCultureIgnoreCase);
                                });
                if (newPageLayout != null)
                {
                    if (publishingPage.ListItem.File.CheckOutType == SPFile.SPCheckOutType.None)
                    {
                        publishingPage.CheckOut();
                    }
                    publishingPage.Layout = newPageLayout;
                    publishingPage.Update();
                    publishingPage.CheckIn("Checked In");
                    publishingPage.ListItem.File.Publish("page published");
                    publishingPage.ListItem.File.Approve("page approved");
                    publishingWeb.Update();

                }

            }


        }

Call the method as below
ChangePageLayoutForDefalutPage(mypublishingwebObj,"myPagelayout.aspx");


Hope this helps..

Popular Posts