Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Heads up, this is very much a beginner question.

My SharePoint experience is primarily with working with SharePoint designer and powershell. I've never touched Visual Studio or anything to use custom code with SharePoint.

I'm wanting to add this code:

/// <summary>
/// Sets the value of an SPBusinessDataField to the newValue.
/// </summary>
/// <param name="item"></param>
/// <param name="fieldInternalName"></param>
/// <param name="newValue"></param>
public static void SetExternalFieldValue(this SPListItem item, string fieldInternalName, string newValue)
{
    if (item.Fields[fieldInternalName].TypeAsString == "BusinessData")
    {
        SPField myField = item.Fields[fieldInternalName];
        XmlDocument xmlData = new XmlDocument();
        xmlData.LoadXml(myField.SchemaXml);
        //Get teh internal name of the SPBusinessDataField's identity column.
        String entityName = xmlData.FirstChild.Attributes["RelatedFieldWssStaticName"].Value;

        //Set the value of the identity column.
        item[entityName] = EntityInstanceIdEncoder.EncodeEntityInstanceId(new object[] { newValue });
        item[fieldInternalName] = newValue;
    }
    else
    {
        throw new InvalidOperationException(fieldInternalName + " is not of type BusinessData");
    }
}

as an extensions method for SPListItem.

How do I go about doing that? Is this achieved using Visual Studio? I can't seem to find any information for complete beginners about how to go about doing this.

Any advice or a push in the right direction would be much appreciated.

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

You would do this just as you would for any other class in c#. Extension methods are .net feature. There is a description of this capability on MSDN: http://msdn.microsoft.com/en-us/library/vstudio/bb383977.aspx.

share|improve this answer
    
Hi there, yes I understand that. My questions was more relating to how I go about taking this class and applying it to SharePoint 2010. As I haven't done custom coding stuff with SharePoint, I'm lacking even that knowledge. Hence the beginners nature of the question. –  MalcolmDurling Feb 19 '13 at 0:16
    
You would deploy the assembly to your SharePoint farm and whenever you would want to use the extension method you would reference the assembly what would make the extension method available for use in your code. –  Waldek Mastykarz Feb 19 '13 at 0:31
    
Ok, thanks for your input. To put this into more context, I want to loop through every item in a list and set the external data column to the value of another string column. I'd be calling this method on each list item. Would it be best to do this in a custom workflow created in Visual Studio? –  MalcolmDurling Feb 19 '13 at 1:03
    
It depends a little on when you want to fire this code up. Workflow might be an option but a TimerJob as well. Could you provide some more information about what you are trying to achieve? –  Waldek Mastykarz Feb 19 '13 at 16:11
    
I have a library with a column of type 'Single line of text' which is populated with property codes. These property codes are now drawn in through an External Content Type and the library has a new column of type 'External Data'. I want to migrate the old column so the property code is copied into external data column. This is something that I'd want to run on a few libraries, but only once. –  MalcolmDurling Feb 19 '13 at 19:46
show 1 more comment

I put all of my extension methods in a "Extensions" project in my solution, along with any other utils, timer jobs, etc. That project is organized with folders something like so...

MyProj.Extensions
--> SPFeatureReceiverPropertiesExtensions.cs
--> SPFileExtensions.cs
--> SPListExtensions.cs
--> SPListItemExtensions.cs

and so forth and so on. Hope this helps.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.