0

I'm using SP2010 Server with C# for development.

I'm currently writing a method that will read a couple of values from each version in a document's history. Ideally I'd like to use the same method but I ran into two issues:

1) I haven't found a way to have the flexibility to pass in either a SPListItem or SPListItemVersion.

This works: getFieldValue(My_SPListItemVersion)

This does not work: getFieldValue(My_SPListItem)

public static void getFieldValue(SPListItemVersion liVersion)
{  
   Console.WriteLine(field.GetFieldValueAsText(liVersion["field1"]));
   Console.WriteLine(field.GetFieldValueAsText(liVersion["field2"]));
   Console.WriteLine(field.GetFieldValueAsText(liVersion["field3"]));
}

2) When I iterate through the version history I can't retrieve the most recent version.

Question
Is there anyway around either one of these or should I stick to writing two methods. One for SPListItem and another for SPListItemVersion.

1 Answer 1

1

It depends on what you want the function to do in the two cases. When called with a SPListItem, if you want it to display the values from the most recent version then one option would be to put all the logic into a function that accepts a SPListItemVersion. If the function was passed a SPListItem then it would just call the function with it's most recent SPListItemVersion.

private void getFieldValue(SPListItem liItem)
{
    //Add in error check to make sure versioning is enabled
    getFieldValue(liItem.Versions[0]);
}

//All the logic goes into this 
public static void getFieldValue(SPListItemVersion liVersion)
{  
   Console.WriteLine(field.GetFieldValueAsText(liVersion["field1"]));
   Console.WriteLine(field.GetFieldValueAsText(liVersion["field2"]));
   Console.WriteLine(field.GetFieldValueAsText(liVersion["field3"]));
}

This should allow you to call the function with either type of parameter and get the expected result.

1
  • Well it looks like the answer was to change my assumption to #2, I didn't think I was returning the current version in the version history but it looks like I was wrong.
    – RWL01
    Jun 30, 2014 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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