Archive

Posts Tagged ‘MOSS 2007’

On-the-fly creation of attachments for list items

August 23rd, 2007 No comments

Here is the code to add an item to a list (task list in this example) programmatically and create and attach a file on the fly. You basically need to encode a set of characters into a sequence of bytes (byte array) and call SPAttachmentCollection.Add method to add the binary representation of your attachment to the list item.

StringBuilder sbLog = new StringBuilder();

sbLog.Append(“Accessing root web of the site collection…\n”);
SPWeb web = new SPSite(“http://moss:21165”).OpenWeb();

sbLog.Append(“Accessing Task list in the root web…\n”);
SPList taskList = web.Lists[“Tasks”];

sbLog.Append(“Adding a new task item…\n”);
SPListItem newTask = taskList.Items.Add();

sbLog.Append(“Populating the fields…\n”);
newTask[“Title”] = “Work hard ,but play harder”;
newTask[“Due Date”] = DateTime.Now;
newTask[“Priority”] = “(1) High”;

sbLog.Append(“Creating the attachment…\n”);
ASCIIEncoding encoder = new ASCIIEncoding();
byte[] bytFile = encoder.GetBytes(sbLog.ToString());
newTask.Attachments.Add(“log.txt”, bytFile);
newTask.Update();

Categories: MOSS 2007 Tags:

Adding and Removing keys from appSettings in web.config

August 12th, 2007 No comments

Today, I was struggling with adding and removing entries from web.config/appsettings in my feature receiver class. Well, it was quite easy to add , but removing was giving me a real hard time. I used my perfect indexing tool and I came across these great posts from Daniel Larson and Tony Bierman. By first look at their posts I realized that removing requires a *right* call to SPWebConfigModification constructor, otherwise it won’t ever happen. Both posts are for adding Ajax http handlers, but one can easily alter them to make the solution work for appsettings as well.

protected void ModifyWebApplication(SPWebApplication app, bool removeModification)
{

SPWebConfigModification modification = new SPWebConfigModification(“add[@key=’TotalDigits’]”, “configuration/appSettings”);
modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode ;
modification.Value = “<add key=\”TotalDigits\” value=\”21\” />”;
modification.Sequence = 0;
if (removeModification)
app.WebConfigModifications.Remove(modification);
else
app.WebConfigModifications.Add(modification);
SPFarm.Local.Services.GetValue().ApplyWebConfigModifications();

}

As Daniel has mentioned in his comments, a real benefit of using the SPWebApplication in your code is that the changes are applied to the farm, since the SPWebApplication represents that virtual web application that lives in the farm context.Thanks Daniel and Tony!

Categories: MOSS 2007 Tags:

SPFeatureDependencyCollection

August 7th, 2007 No comments

You can programmatically find all activated features that are dependent on a specific feature by using the following example. In this example, feature is stopped from being deactivated because dependant features are still in active mode.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Collections;
using System.Diagnostics;

namespace Configuration
{
public class Configurator : SPFeatureReceiver
{
public override void FeatureInstalled(SPFeatureReceiverProperties properties) {}
public override void FeatureUninstalling(SPFeatureReceiverProperties properties) {}
public override void FeatureActivated(SPFeatureReceiverProperties properties) {}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
        SPSite site = properties.Feature.Parent as SPSite;
        SPFeatureCollection spfeatCol = site.Features;
        //iterate through all the features of the current site collection
       foreach (SPFeature feature in spfeatCol)
          {

          // Get the collection of all features that are dependent on this feature
         SPFeatureDependencyCollection depCollection = feature.Definition.ActivationDependencies;
         foreach (SPFeatureDependency featureDependency in depCollection)
                {
                if (featureDependency.FeatureId.Equals(properties.Feature.DefinitionId) && feature.Definition.Status.Equals(SPObjectStatus.Online))
                        {
                         throw new Exception(“Deactivation aborted! There is at least one dependant feature in active mode.”);
                         }
                 }
           }

// Do the rest of the work here.

}
}
}

Categories: MOSS 2007 Tags:

non CLS-compliant classes in SDK, what that means?

July 30th, 2007 No comments

When you browse SharePoint SDK , you come across some classes such as ContentByQueryWebPart which is marked as non CLS-compliant classes. This class or any derived type may not be interoperable with a wide range of .Net supported programming languages.Something that you really need to consider when using this calss or driving your custom types off of it. Generally, C# complier does not check for CLS compliance of your code and you have to explicitly instruct it to do so.

This class for whatever reason is not decorated to be CLS-compliant and it should be used with extra caution in regards to interoperability.

[CLSCompliant(false)]
public class ContentByQueryWebPart : CmsDataFormWebPart
{
…..
}

Categories: MOSS 2007 Tags:

External Hard Drives and SharePoint

July 30th, 2007 No comments

I have used both types of external drives (USB2 and 1394 connections) for my SharePoint images and the USB2 drives always perform better than firewire drives
for me. 1394 drives sometimes give me headaches after restart and I basically have to unplug and plug it back in ,so keep this in mind as the first tip.

SharePoint is a resource intensive product and on the top of that a virtualized SQL server adds too much overhead so you can never expect
the same performance and transfer rate you get with IDE/SCSI.None of the existing external hard drives are meant to give you such a performance.

If you are performing backups as well then consider getting two drives, one dedicated to your sharepoint images and the other one for storing your backups.
I will never store backups side-by-side my images.A large external hard drive ( 250GB or greater) is necessary.

Remember, Most of external hard drives come pre-formatted so always check to see what file format is used at first place before you start creating your demo or non-demo images.
By creating a VHDs on FAT32 ,Virtual PC splits it to multiple files which may not be what you want so you have to take extra steps to merge them all.

At the end and for Vista Users in particular, always make sure that the external hard drive you are about to purchase has no compatibility issue with your OS.
I know some people who had problem hooking up their Western Digital 500 GB External HD and they are still stuck!

Categories: MOSS 2007 Tags: