Saturday, February 12, 2011

Deploy UserControl to Sharepoint



Here are the steps to deploy.
1. Create a WSPBuilder Project in Visual Studio 2008

2. Add folders to the Project which looks similar to the 12 hive. Put your UserControl (.ascx file) in the ControlTemplates of the 12 hive.


Here, TestUC is the UserControl and is added to the ControlTemplates folder I created. I idea behind this is, once this is deployed, as per the folder structure, the TestUC will be deployed to the ControlTemplates folder in the 12 hive.

Also add the elements.xml and the features.xml to the folder (name of the feature, in this case it is DeployUC)

3. Probably the most important of the steps. Open elements.xml and add the UserControl element to the elements.

3. Create a Code folder and add the TestUC.ascx.cs file. We will link the UserControl to its code file using the Inherits property for the UserControl.


4. Open TestUC.ascx and link the Test.ascx.cs file

If you see, DeployUC.Code is the Namespace under DeployUC assembly under which TestUC (the code-behind file for the UserControl) resides. One this feature is deployed, this assembly sits in GAC and can always be found by the TestUC UserControl.

5. Build and deploy the package and this UserControl is now available for use on to any page.

6. Done.

Post your comments and questions if you have any.

Tuesday, February 8, 2011

GUID

Is GUID unique? While each generated GUID is not guaranteed to be unique, the total number of unique keys (2^128 or 3.4×10^38) is so large that the probability of the same number being generated twice is very small.

GUID is a 16 byte (or 128 bits) combination of alphabets and numerics. Once you take it apart, the bits of the GUID break down like this:
  • 60 bits of timestamp,
  • 48 bits of computer identifier,
  • 14 bits of uniquifier, and
  • 6 bits are fixed,

for a total of 128 bits.

More information can be found here.

Tuesday, February 1, 2011

'The database principal owns a schema in the database, and cannot be dropped' error in SQL Server 2005 & 2008

I ran into this situation today. I created a User, made him dbOwner to a database DBX. I no longer needed this User and so tried deleting and got this error.

Drop failed - The database principal owns a schema in the database, and cannot be dropped.

After Googling for sometime, I found that this User called as "Database Principal" has "db_owner" role selected for the schema. This means the Database Principal I want to delete owns the schema.

One solution is to delete the schema and then delete the User. Another Solution was to change the DB_Owner to some another "Database Principal" - to "dbo". I did it and then I was able to delete the user/Database Pricipal successfully.

To change the DB_Owner to some another "Database Principal"; simply Drill Down to your Database in Sql Server Management Studio and further more Drill Down to your_DB_Name > Schemas > db_owner > right click > select properties. You would find the name of the "Database Principal" that you want to delete. Change this to some another "Database Principal"; for Example, to "dbo".

Reference here

Thursday, January 20, 2011

Add file to List (or) File Upload to a Sharepoint list

Here is the code piece to handle file upload to a Sharepoint list.

SPSite site = SPContext.Current.Site;
using (SPWeb web = site.OpenWeb("MySite"))
{                
    SPFolder targetFoler = web.Lists["Shared Documents"].RootFolder;
    Stream fStream = fileUploader.PostedFile.InputStream;
    byte[] contents = new byte[fStream.Length];

    fStream.Read(contents, 0, (int)fStream.Length);
    fStream.Close();

    targetFoler.Files.Add(fileUploader.FileName, contents);                
}

Hope this helps.

Code snippets in Toolbox

This is pretty useful tip for developers. In Visual Studion 2005 and later, you can drag text from source window onto Toolbox and drag it back wherever and whenever you want.

1. Select the text in source window
2. Drag the selection onto Toolbox tab (assuming it is expanded)
3. You will see a new item in the Toolbox

4. You can 'Rename item' on right click.


This is a very useful tip for those who frequently use some definite code pieces.

Hope this helps.

Assign User Group as owners to Sharepoint site

Here is the logic to assign a User Group as owners to a site.

using (SPWeb web = site.OpenWeb("MySite"))
{
    SPWeb parentWeb = web.ParentWeb;
    SPGroup ownersGroup = parentWeb.SiteGroups["SomeOwnersGroup"];
    web.AssociatedOwnerGroup = ownersGroup;

    web.BreakRoleInheritance(true);

    SPRoleAssignment roleassignment = new SPRoleAssignment(ownersGroup);
    SPRoleDefinition roleDefinition = parentWeb.RoleDefinitions["Full Control"];
    roleassignment.RoleDefinitionBindings.Add(roleDefinition);
    
    web.RoleAssignments.Add(roleassignment);

    web.Update();
}

Hope this helps.

Sharepoint RoleAssignement exception "This operation is not allowed on an object that inherits permissions."

If you are getting This operation is not allowed on an object that inherits permissions. exception when setting roles (User groups to website), here is the fix.

You need to break the permission inherited by the subsite from sitecollection. This will let you set Users and Groups independently for the subsite. Here is the code piece.

using (SPWeb web = site.OpenWeb("MySite"))
{
    web.BreakRoleInheritance(true);
    //Rest of RoleAssignment logic
}

Hope this helps.