Showing posts with label MOSS Development and Deployment. Show all posts
Showing posts with label MOSS Development and Deployment. Show all posts

Wednesday, May 30, 2012

Property bags in SharePoint


Property bag in SharePoint exist in MOSS version and even continued to be a part of 2010 version. Property bag is nothing but a key value pair stored as hash table that we can retrieve in object model.

We can add, edit or remove properties from property bag from SharePoint Designer or through object model.

So let’s go ahead and check that out in Designer. Connect with the site.

Check out site options and click on it.

Here you can see parameters; these are the properties for this site. Here we can add, edit and remove. Let’s go ahead and add three properties and values.

Click on Add give name and value.



In the same way create two more properties.

Before we dive into coding part, I would like to bring in some more details.

We can define properties for several levels in SharePoint. At highest level we can have it for SPFarm, then SPWebApplication, then for SPSite, for SPWeb and for SPList.

Let’s us start by adding and retrieving properties that we just defined at web level.

  protected void btnPropertyBag_Click(Object sender, EventArgs e)
        {
            SPWeb objWeb = SPContext.Current.Web;
            objWeb.Properties.Add("CustomKey4AddedFromCode", "CustomValue4AddedFromCode");  
            objWeb.Properties.Update();  

            string strProp =  objWeb.AllProperties["CustomKey1"].ToString();
            strProp = objWeb.AllProperties["CustomKey2"].ToString();
            strProp = objWeb.AllProperties["CustomKey3"].ToString();
           
        }  



Sometime you can have Properties and not AllProperties like for SPlist, , SP web application and SP farm.

You can also remove property that we’ve added.

  objWeb.Properties.Remove("CustomKey4AddedFromCode");


If you want to check if the key exist, use containskey

if(objWeb.Properties.ContainsKey(""))


If you want to add it to the list or library, then you won’t find properties collection directly. You need to take rootfolder object and store it there. Like shown below.

SPWeb objWeb = SPContext.Current.Web;

SPList lstCalendar = objWeb.Lists.TryGetList("Calendar");

lstCalendar.RootFolder.Properties.Add("CustomCalendarKey", "CustomCalendarValue");

For SPSite there is no as such property bag available, but if you want to store, then use rootweb for SPSite and then assign properties. Like shown below.

SPWeb objWeb = SPContext.Current.Web;

SPSite objSite = objWeb.Site;

objSite.RootWeb.Properties.Add("SiteLevelKey", "SiteLevelValue");

For Web Application you can use this

SPWeb objWeb = SPContext.Current.Web;

             Microsoft.SharePoint.Administration.SPWebApplication objWebApp = objWeb.Site.WebApplication;

             objWebApp.Properties.Add("WebAppLevelKey", "WebAppLevelValue");

 Hope this will help to understand property bag.

Tuesday, May 15, 2012

How Modify MasterPage or Pagelayout programmatically? (Especially when it is in use)


Most of the time we are using SharePoint Designer for master page and page layout deployment, which is fine for first time going live because for the first time we mostly used backup restore or other stuff.

But problem occurs when you want to update master page or page layout in subsequent release. We can do that easily with SharePoint designer but some unlucky fellow like us who do not have access to production will have to find some workaround for that. And if master page and Page Layout  being used then it will not allow to delete and add it again.

Here are simple steps what we had done

  • Add physical copy of master page/page layout in your project (or put it in 14 hive)
  • Create a feature receiver which will read that file added in 14 hive, read as binary stream.
  • Find master page in master page gallery (_catalog folder)
  • If (not found) then add new master page/layout (will be used for first time deployment)
  • If (found) then open/read master page/layout from library
  1. Check out
  2. Open as binary form and copy binary object from physical copy
  3. Commit transection, so new version will be added for master page/layout.
  4. Check in
  5. Publish
This way if master page is currently used in site or page layout has been used any of the page you can easily update it from code.

 It will not require access to production. You can add feature receiver in WSP and activate it on deployment; and you are done.

We will provide code snippet for this soon :)

Wednesday, July 15, 2009

Designing anonymous application page in SharePoint

Hi All,

Today I am going to discuss on how to design anonymous access application page. This is a common problem that many company developer faces. They always ask that designing such a page is always difficult. You will have site running in windows authentication or in forms authentication. If the entire site is anonymous then it is altogether a different story.

So let us discuss how we can design such application page that can be accessed by the anonymous users as well.

Normally we inherit the application page by LayoutPageBase class, right? Yes, so first thing to note is instead of inheriting from this class, we are required to inherit from class named UnsecuredLayoutsPageBase.

In addition to this, you also need to override one more method which is

protected override bool AllowAnonymousAccess
{
get
{
return true;
}
}


Once you are done with these two settings, you are done with this. Try to achieve this and let me know your feedback.

Thank you

Wednesday, December 17, 2008

Create Custom Web Part in SharePoint

Hello Everyone !

Are you new to SharePoint development? Do you want to see how to create a custom web part?

Let me explain you from basic. Although there are many ways to create a web part but lets look at one way.


Following steps are involved in the creation of a custom web part:
1. Creation of Web Part project.
2. Creation of controls used in the web part.
3. Loading the control as web part.
4. Deploying the web part code in the SharePoint site.
5. Adding the web part on the site page.

Let’s understand each step with the help of an example.

Step 1: Open Visual Studio and create a new blank solution as shown in figure. 1.





Figure.1
Step 2: Add a new project of type web application under the newly created solution as shown in the figure.2 below.




Figure.2

Delete the existing class from it. Add a new web user control in this project and give some appropriate name to it as shown in figure. 3




Figure .3
Code snippet for the user control and its code behind is given below:



using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace MyWebControls
{
public partial class MyUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void MyButton_Click(object sender, EventArgs e)
{
Response.Write("Good Morning " + txtUserName.Text);
}
}
}

Step 3: Create a new project under the solution of type class library as shown in figure.4 below.





Figure. 4

Delete the default class and add a new class as shown in the figure.5





Figure .5

Add Reference of Windows SharePoint Services to this project as shown in the figure. 6 below.




Figure.6

Code snippet is shown below:
Generate a new GUID.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Runtime.InteropServices;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace MyWebPart
{
[Guid("52D7FA40-EF93-45ad-806E-9B77C782A93B")]
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Assert, Unrestricted = true)]
public class MyWebPartClass : System.Web.UI.WebControls.WebParts.WebPart
{
public MyWebPartClass()
{

this.ExportMode = WebPartExportMode.All;
}

string virtualpath = @"~/UserControls/MyUserControl.ascx";
protected override void CreateChildControls()
{
MyWebControls.MyUserControl objMyUserControl = (MyWebControls.MyUserControl)Page.LoadControl(virtualpath);
this.Controls.Add(objMyUserControl);
}

}
}

Step 4: Go to the properties of the web part project and select the build output path to the bin folder of the web application virtual directory. Before compiling add reference of the web part control project to it.

Add a safe control entry (below the safe control entries which are already present) to the web.config file of the web application which is present inside the virtual directory.





Change the trust level to Full as shown below.




Step 5:
Add Control to site:

Create a new folder within the virtual directory of the web application to store the user control. Give it a name such as UserControls.

Now go to Site Settings and click web parts as shown in the figure below



Figure.7
Click on New Web parts. Select the web part we deployed and click populate gallery.





Figure.8

Go to the page on which you want to add the web part. Click the site settings àEdit Page as shown in the figure. 9 below.





Figure.9

Select the web part from the list of web parts. That’s it your web part is ready to play its role.




Figure.10



Figure.11

I hope this post will help many developers. If you still need more clarification please write to us at

sharepointkings@gmail.com
We will be happy to help you as always.






Monday, September 29, 2008

MOSS deploy dll in bin folder

This article i hope will help many starters as well as experienced SharePoint developers to understand the way SharePoint uses the dll deployed in its site directory bin folder which is located under the inetpub/virtual directory folder. As we all know and have used mostly (atleast in our case) deployment of dll (Custom WebPart, Custom control etc) in GAC (Global Assembly Cache) was the way to develop SharePoint solutions. But there is another very good approach which is mentioned below in steps. That is to deploy the dll directly to the site bin folder. Here are the steps to accomplish:

1. Set Output path of the projects (Project in which you are keeping webpart control classes) to the site bin folder. You can find the path by navigating to C:\Inetpub\wwwroot\virtual directory\port number\bin

2. Build the solution and make sure it is compiling successfully.
3. Add a safe control entry to the web config file. Do not mention public key token. Do not strong name the project.
4. That's it, your code is deployed in the site's bin directory and its information is located in the config file. Go Ahead and populate the web part gallery with the web part and add it  to a page.

Advantages:
1. Biggest advantage is the fact  that you do not have to reset iis after every compilation of code.
2. You can directly copy the dll from the other bin folders to this as and when required.

DisAdvantage
1. As you are deploying code to the bin folder of particular site, it will not be available globally. That means you will need to add the dll to every site's bin folder.

I hope this article will clear most of your doubts. In case something is not understood, do write a comment to this post or email us at sharepointkings@gmail.com




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com