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.