Showing posts with label Sharepoint. Show all posts
Showing posts with label Sharepoint. Show all posts

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.

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.

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.