Tuesday, April 19, 2011

Delete all table in a Database

EXEC sp_MSforeachtable @command1 = "DROP TABLE ?"

Deletes all tables and this operation can also be rolled back!!

Thursday, March 10, 2011

Authenticate external users to Sharepoint 2010.

Here is an article briefing the way to create a custom Trusted Identity Provider using the new Windows Identity Framework in Sharepoint 2010.

http://stefvanhooijdonk.com/2010/08/19/writing-your-own-trusted-identity-provider-for-sp2010-1/

Using this provider, external users like Users in a Sql Server, Facebook or any source will be able to login to Sharepoint 2010 and not just AD users. Cool!

Wednesday, March 2, 2011

Events in Sharepoint 2010

Here are the events available in Sharepoint 2010 which gives developers better control when things are happening/about to happen in the site.

Items in green are new to Sharepoint 2010 and are Asynchronous events.

Site Events
  • SiteDeleting/SiteDeleted
  • WebDeleting/WebDeleted
  • WebMoving/WebMoved
  • WebAdding/WebProvisioned

List Events
  • EmailReceived
  • FieldAdding/FieldAdded
  • FieldUpdating/FieldUpdated
  • FieldDeleting/FieldDeleted
  • ListAdding/ListAdded
  • ListDeleting/ListDeleted

Item Events
  • ContextEvent
  • ItemAdding/ItemAdded
  • ItemUpdating/ItemUpdated
  • ItemDeleting/ItemDeleted
  • ItemCheckingIn/ItemCheckedIn
  • ItemCheckingOut/ItemCheckedOut
  • ItemUncheckingOut/ItemUncheckedOut
  • ItemAttachmentAdding/ItemAttachmentAdded
  • ItemAttachmentDeleting/ItemAttachmentDeleted
  • ItemFileMoving/ItemFileMoved
  • ItemFileConverted

Workflow Events
  • WorkflowStarting/WorkflowStarted
  • WorkflowPostponed
  • WorkflowCompleted

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.

Thursday, January 13, 2011

WSP builder error - User xxxx does not have installation permissions on xxxx server. Preflight requirements failed.

When trying to deploy a package to the server, WSP builder throws an exception

User xxxx does not have installation permissions on xxxx server.
Preflight requirements failed.


Let me tell you, this is not an exception. What it means is you do not the required permissions on the sharepoint server to perform this operation.
You can do these things to fix it.

1. See if you are added to Site collection administrator
2. And the important one, make sure you have permissions at the DB level. You should have permissions on SharePoint_Config Database on the server.

This should fix the problem.

Monday, January 10, 2011

ASP.NET Website. What, Why and How.

Everybody needs to start with this one. Atleast who wish to learn ASP.NET. So, how to create a website. Well, there are many ways. One simple way is to trust Visual Studio 2005 or 2008 or 2010.

1. Open Visual Studio -> File -> New Website.
2. In the New website window, if you wish to host the website on IIS, choose Http for Location and http://localhost/Website
3. Choose your language and hit OK.
Its done.

What it does for you is, you will see a couple of files VS adds to your website by default.
1. Default.aspx
2. Web.config

Default.aspx is a webpage like any other html page you visit and its the .NET version for the same

The website is hosted on IIS. Great! But where on the file system? By default, VS creates all websites (when location is set to Http) at C:\inetpub\wwwroot. What does this mean?

Apart from this website, you can create multiple websites on IIS (the server) and all will go to the same C:\inetpub\wwwroot but exist as different Virtual Directories. Note this down. You will hear a lot about this during your dev days. Coming back, you can still call your new website as a Website. Its just that it exists as a separate virtual directory under IIS.

One thing here. IIS listens on port 80. And there could be many ports on a machine. For example, a SQL Server listens on 1433. And so on.

So, when you try to access your website http://localhost/website, you are sending a request to IIS which is the localhost listening on port 80. Similarly all the websites that you create on IIS can be accessed via the same http://localhost/anyotherwebsite.

Ok. Enough of this. Any other way of creating a website? Yes, the other way around. Here it is.

1. Create a folder anywhere in your system and name is NewWebSite.
2. Open IIS. (Tip: Press Windows + R. Type inetmgr and hit enter)
3. Expand left tree node till you see Default Web site.
4. Right click and select New Virtual Directory
5. Give a name to the website (preferably the same name as the folder 'NewWebSite')
6. Browse to the newly created folder NewWebsite for PhysicalPath and hit OK.

Your new website is created in IIS. Now, open Visual Studio -> File -> Open Website -> Choose IIS -> Select NewWebsite from the list.

This opens a website in Visual Studio but with no files under it in the solution explorer. This is unlike the first case where you have seen Default.aspx and Web.config already added to the website. RIght click the website in Solution Explorer and add the items that you wish. You can even add Madonna.aspx to your new website. And Madonna can accessed at http://localhost/NewWebSite/Madonna.aspx

The great thing about this approach is you just knew that, a website is nothing but a folder of files. Files could be aspx, doc, htmls, images, css, js, mp3, mpg, flv, config and whole lot of other files. The thing that you should understand is, a website is nothing but a folder with files and like any other folder that you copy and carry in a USB stick, a website too can be moved from a computer to a USB stick, from one computer to another, from a developer machine to production server and virtually to anywhere. You just need to know how to attach a website to IIS which is just described above.

Let me know if you have any questions.