Monday, May 23, 2011

Send an email with html format using SPUtility.SendEmail

SPWeb oWeb = SPContext.Current.Web

StringDictionary headers = new StringDictionary();

headers.Add("from",  "system@domain.com");
headers.Add("to", spUser.Email);
headers.Add("subject", "Welcome to the SharePoint group: ABC site: ");
headers.Add("content-type", "text/html"); //This is the default type

System.Text.StringBuilder strMessage = new System.Text.StringBuilder();                  
strMessage.Append("<br><br><b>Login Instructions: </b><br>");
strMessage.Append("<font color='red'><UL><li>If you are a US employee ALWAYS login in using the following login format </font><br>");

SPUtility.SendEmail(web, headers,strMessage.ToString());

Tuesday, May 17, 2011

Programmatically set permissions to a folder in a SharePoint list

This method can be used to bind permission to a folder in a SharePoint list

 /// <summary>
/// Method to set the permission at folder level for the newly created site groups
/// </summary>
/// <param name="web">The current web instance</param>
/// <param name="groupName">Name of the Site group that is added to the folder</param>
/// <param name="listName">Name of the list</param>
/// <param name="folderName">Name of the folder in the list</param>
/// <param name="role">Role/permission level set to the group to access folder for the site group users</param>

 private void SetRoleDefinitionBinding(SPWeb web,string groupName, string listName, string folderName, string role)
        {
            try
            {
                SPRoleAssignment roleAssignment = new SPRoleAssignment((SPPrincipal)web.SiteGroups[groupName]);
                SPList list = web.Lists[listName];
                SPQuery query = new SPQuery();
                    query.Query = "<Where><Eq><FieldRef Name='Title'/><Value Type='Text'>" + folderName + "</Value></Eq></Where>";
                //Retrieve the items based on Query
                SPListItemCollection items = list.GetItems(query);
               
                //Get the name and Url for the folder
                foreach (SPListItem item in items)
                {
                    SPFolder folder = web.GetFolder(item.Url);
                    folder.Item.BreakRoleInheritance(true);
                    roleAssignment.RoleDefinitionBindings.Add(web.RoleDefinitions[role]);
                    folder.Item.RoleAssignments.Add(roleAssignment);
                    web.Update();
                }
      
            }
            catch (Exception ex)
            {
              
                //Error handling
            }
        }

Tuesday, May 10, 2011

Activity 'onWorkflowActivated1' validation failed: Cannot resolve Activity 'Workflow1'

I got following error when I try to build custom workflow in Visual Studio 2008
"Activity 'onWorkflowActivated1' validation failed: Cannot resolve Activity 'Workflow1'"

To resolve this, do a search in your entire project and find  any references to "Workflow1", if so, rename all the occurrences with the modified class name.

Friday, May 6, 2011

"Save library as template" link is missing in SharePoint site

Some of the document libraries and list, "Save library as template" link is hidden by default. If you go to  "View All Site Content" and have a look at the description next to each library/list, the description that says "This system library was created by the Publishing feature.." will have the "Save library as template" link hidden.

This behaviour is by default, however we have a workaround for this. Go to the "Document Library Settings" page. You should see the URL address like this ".../listedit.aspx?List={GUID}", change the "listedit" to "savetmpl" and the URL look like this "../savetmpl.aspx?List={GUID}", now you will be able to save the library as a template.

This method is not recommended, but it works. If you have any workflow bound to the library then you may have some issues,but works fine if you just want to create a template for document settings, Columns and views.

Thursday, May 5, 2011

To prevent users from creating folders in SharePoint document libraries

Using SharePoint 2007;

The OOB method to remove the "New Folder" menu will prevent users only creating folder from the standard view and users are still be able to upload/create folders from explorer view. Below is the feature code used to prevent users from creating folders in document library even in explorer view.

Feature.xml

<?xml version="1.0" encoding="utf-8"?>
<Feature  Id="62b28298-e3fc-4ccc-a2ea-3f92b7e9a21e"
          Title="Disable Folders"
          Description="Feature to disable creating new folders in document libraries in the site"
          Version="12.0.0.0"
          Hidden="FALSE"
          Scope="Web"
    ActivateOnDefault="FALSE"
          DefaultResourceFile="core"
          xmlns="http://schemas.microsoft.com/sharepoint/">
  <ElementManifests>
    <ElementManifest Location="elements.xml"/>
  </ElementManifests>
</Feature>


Elements.xml

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
 <Receivers ListTemplateId="101">
  <Receiver>
   <Name>AddingEventHandler</Name>
   <Type>ItemAdding</Type>
   <SequenceNumber>10000</SequenceNumber>
   <Assembly>AAAA.BBB.AllUsers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f4da00116c38ec5</Assembly>
   <Class>AAAA.BBB.AllUsers.DisableFoldersInLib</Class>
   <Data></Data>
   <Filter></Filter>
  </Receiver>
 </Receivers>
</Elements>

Code:
 public override void ItemAdding(SPItemEventProperties properties)
        {
            base.ItemAdding(properties);
            //If the new created file type is "folder" then the following conditions will be true
            if ((properties.BeforeUrl != properties.AfterUrl &&
                properties.ListItem == null &&
                properties.ListItemId == 0) ||
                properties.AfterProperties["ContentType"].ToString() == "Folder")
            {
                properties.Cancel = true;
                properties.ErrorMessage = "You are not allowed to create new Folders in this document library. Please tag the files with Metadata instead of creating folders.";
            }
        }

The source code of this feature is available at Technet Gallery

Thursday, April 14, 2011

Parent child relationship for lists in SharePoint

If you are looking for a project/child relationship for a Projects and Tasks list.  You can try using the
Budgeting and Tracking Multiple Projects Template which is part of the SharePoint "Fantastic 40" Site Templates. Basically this template allows you to track all your projects and tasks. When you view the project item, you also get a Related tasks, Related issues and Related milestones data views on the same page.Any tasks, issues or milestones you add on this page will be associated with that particular project and will rollup onto a nice looking dashboard.

Refer to the following URL to download the template.
http://www.microsoft.com/downloads/en/details.aspx?FamilyId=04FDA604-BAB0-4E43-8B88-38101DFE121A&displaylang=en

Refer to the following site which is created using this template.
http://demo.sharesquared.com/PTPM/BTMP/default.aspx


In case if you want any extra customization, you can customize this site and save as a template to reuse it.