Friday, 19 March 2010

SharePoint 2007 Feature Deployment - Creating Lists with Data Programatically versus Declaratively

To simplify SharePoint deployment, it is a good practice to deploy supporting components such as lists as part of the same feature (so they don't need to be created manually).

While it IS entirely possible to deploy list instances WITH data with a declarative ListInstance block as below (see xml snippet below, and see http://msdn.microsoft.com/en-us/library/ms478860.aspx), the drawback is that you have little control over how the data is actually deployed to your lists. For example, if I deploy the package twice, it will simply double-up on the data - which is not an ideal scenario.

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <!-- NOTE: FeatureId is the feature where the list template is defined, 
  NOT the current featureId. Template Type should match the list template type
  -->
  <!--Create the list instance-->
  <ListInstance FeatureId="a0bf89ed-c424-4391-ba75-0e43d3391bdb" 
                Title="SAPState" Description="SAP State List (Created by a Feature)" 
                Id="1099" TemplateType="100" Url="Lists/SAPState">
    <!--Add the data -->
    <Data>
      <Rows>
        <Row>
          <Field Name="ItemId">NSW</Field>
          <Field Name="Title">New South Wales</Field>
          <Field Name="DisplayOrder" />
        </Row>
        <Row>
          <Field Name="ItemId">ACT</Field>
          <Field Name="Title">ACT</Field>
          <Field Name="DisplayOrder" />
        </Row>
        <Row>
          <Field Name="ItemId">VIC</Field>
          <Field Name="Title">Victoria</Field>
          <Field Name="DisplayOrder" />
        </Row>
      </Rows>
    </Data>
  </ListInstance>
</Elements>

To work around this issue, I recommend that you make use of a SPFeatureReceiver and use code to programatically control the creation of your lists and list data. See the helper method below for an example. Note that you retrieve the list template you want to use via the SPWeb.ListTemplates[] collection

/// 
        /// Creates custom list using the specified template and adds lookup data as neccessary
        /// 
        /// /// /// /// /// /// 
        public static Guid ProvisionCustomList(SPWeb web, string listName, string listDecription, SPListTemplate listTemplate, List listData)
        {
            SPList tryList = null;
            Guid listID = Guid.Empty;
            try
            {
                tryList = web.GetList(listName);
            }
            catch
            { }

            if (tryList == null)
            {
                try
                {
                    listID = web.Lists.Add(listName, listDecription, listTemplate);
                }
                catch (Exception ex)
                {
                    throw new Exception("Cannot instantiate the list '" + listName + "'. " + ex.Message, ex);
                }

                if (listID != Guid.Empty && listData != null && listData.Count > 0)
                {
                    web.AllowUnsafeUpdates = true;
                    var list = web.Lists[listID];
                    try
                    {
                        foreach (var dataRow in listData)
                        {
                            var listItem = list.Items.Add();
                            listItem["ItemId"] = dataRow.ItemId ?? "";
                            listItem["Title"] = dataRow.Title ?? "";
                            listItem["DisplayOrder"] = dataRow.DisplayOrder ?? "";
                            listItem.Update();
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("Cannot add the data to the list '" + listName + "'. " + ex.Message, ex);
                    }
                }
            }

            return listID; 
        }

Thursday, 18 March 2010

List of Permissions Required for Deployment and Activation of Solutions/Features on a SharePoint Site

Today I had some issues today with deployment of SharePoint Solutions/Features to one of the development servers. I knew you need both database and site permissions - but wasn't clear on exactly what databases I needed permissions to. Here are the permissions that you need to deploy a solution/feature to SharePoint:
  1. Farm Administrator (add this through Central Admin)
  2. Site Collection Administrator (Done through the Individual Site Collection itself)
  3. db_owner and db_securityadmin permissions on the Central Admin Content database (note that this is normally generated with a Guid in the name e.g. SharePoint_AdminContent_XXXX-XXX-XXX-XXXX.
  4. db_owner permissions on the Content database for your site e.g. DEV_MachineName_WSS_Content_Portal_5000_0
  5. Local admin rights on the server itself.

Sunday, 14 March 2010

Fix for issue with Vista which prevents machines from going to sleep - related to Multimedia Sharing

It must have been a patch or hotfix that Windows Update installed recently (or the fact that it detected my Windows 7 Ultimate install's multimedia sharing capabilities), but my wife's laptop with Vista Home would never go to "Sleep" (aka suspend mode) when the lid was closed or the "Sleep" option was chosen from the Shutdown prompt.  If you choose the sleep option, it would just wait a couple of seconds, jump to a login prompt but never actually get to the Shutdown/Sleep sequence. The fix was as per:
http://www.techsupportforum.com/microsoft-support/windows-vista-windows-7-support/143828-vista-will-not-sleep.html

It was something to do with the "Multimedia Sharing Options" which stop the machine from going to sleep:

To fix the issue, do this:


Control Panel -> Power Options -> If "High Performance" is selected, choose something else, like "Balanced" -> Change plan settings (for the chosen power plan) -> Change advanced power settings -> Scroll down to Multimedia settings -> When Sharing Media -> Set all options to "Allow the computer to sleep".


The machine now sleeps when the lid is closed and boots up when opened (as before). A suggestion to MS would be to show a message in the Windows messagebar explaining why the machine was prevented from going to sleep rather than just flat out refusing to respond to "Sleep" requests.

Friday, 12 March 2010

InfoPath 2007 - Fix for "Operation could not be completed" errors when opening InfoPath form in design view

If your InfoPath 2007 form suddenly stops working in design mode and you start to get the following error when opening the form in design mode:

"Operation could not be completed."

There are a few things you can do to try and fix it:
  1. Remove all the the local cache files from your machine (typically found in C:\Users\USERNAME\AppData\Local\Microsoft\InfoPath) - delete the "Designer2", "Cache2" and temp folders
  2. If that doesn't work, try running the form (e.g. in Visual Studio 2008, just right click on the form project and Click "Debug -> Start New Instance". When it opens, it will correct the project structure or re-add neccessary .NET assemblies to the project. Go to design mode again - and it will all start working (though all the tabs in the Designer window will now open). I suspect this happens if the xsf file is not correctly written to or some of your supporting files are readonly on save.