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

I'm using CKSDev spmetal generator. I have set up the following parameter file :

<Web xmlns="http://schemas.microsoft.com/SharePoint/2009/spmetal"
     Class="ModelContext">
  <ContentType Name="Item">
    <Column Name="Editor" />
    <Column Name="Author" />
    <Column Name="Created" />
    <Column Name="Modified" />
    <IncludeHiddenColumns/>
  </ContentType>
  <IncludeHiddenContentTypes/>
</Web>

The Item class is created as expected, but the columns Editor, Author, etc. are not generated.

I've also tried specify one of my custom content type in place of Item, but these fields are still not generated.

What am I missing ?

share|improve this question
    
were you able to resolve this? –  Ryan Erickson Apr 19 '13 at 16:18
add comment

1 Answer

up vote 3 down vote accepted

I got around this by extending the Item class in a new file because passing the <column name="" /> in the xml file and also trying <IncludeHiddenFields/> node did not work. This blog explains the process for extending the Item class.

Here is an example of the code though you will need to add it to whatever namespace you are using for the rest of the generated classes:

public partial class Item : ICustomMapping 
{ 
    [CustomMapping(Columns = new String[] { "Modified", "Created", "Editor", "Author" })] 
   public void MapFrom(object listItem) 
    { 
        SPListItem item = (SPListItem)listItem; 
        this.Modified = (DateTime)item["Modified"]; 
        this.Created = (DateTime)item["Created"]; 
        this.CreatedBy = (string)item["Author"]; 
        this.ModifiedBy = (string)item["Editor"]; 
    }

    public void MapTo(object listItem) 
    { 
        SPListItem item = (SPListItem)listItem; 
        item["Modified"] = this.Modified; 
        item["Created"] = this.Created; 
        item["Author"] = this.CreatedBy; 
        item["Editor"] = this.ModifiedBy; 
    }

    public void Resolve(RefreshMode mode, object originalListItem, object databaseObject) 
    { 
        SPListItem originalItem = (SPListItem)originalListItem; 
        SPListItem databaseItem = (SPListItem)databaseObject;

        DateTime originalModifiedValue = (DateTime)originalItem["Modified"]; 
        DateTime dbModifiedValue = (DateTime)databaseItem["Modified"];

        DateTime originalCreatedValue = (DateTime)originalItem["Created"]; 
        DateTime dbCreatedValue = (DateTime)databaseItem["Created"];

        string originalCreatedByValue = (string)originalItem["Author"]; 
        string dbCreatedByValue = (string)databaseItem["Author"];

        string originalModifiedByValue = (string)originalItem["Editor"]; 
        string dbModifiedByValue = (string)databaseItem["Editor"];

        if (mode == RefreshMode.OverwriteCurrentValues) 
        { 
            this.Modified = dbModifiedValue; 
            this.Created = dbCreatedValue; 
            this.CreatedBy = dbCreatedByValue; 
            this.ModifiedBy = dbModifiedByValue; 
        } 
        else if (mode == RefreshMode.KeepCurrentValues) 
        { 
            databaseItem["Modified"] = this.Modified; 
            databaseItem["Created"] = this.Created; 
            databaseItem["Author"] = this.CreatedBy; 
            databaseItem["Editor"] = this.ModifiedBy; 
        } 
        else if (mode == RefreshMode.KeepChanges) 
        { 
            if (this.Modified != originalModifiedValue) 
            { 
                databaseItem["Modified"] = this.Modified; 
            } 
            else if (this.Modified == originalModifiedValue && this.Modified != dbModifiedValue) 
            { 
                this.Modified = dbModifiedValue; 
            }

            if (this.Created != originalCreatedValue) 
            { 
                databaseItem["Created"] = this.Created; 
            } 
            else if (this.Created == originalCreatedValue && this.Created != dbCreatedValue) 
            { 
                this.Created = dbCreatedValue; 
            }

            if (this.CreatedBy != originalCreatedByValue) 
            { 
                databaseItem["Author"] = this.CreatedBy; 
            } 
            else if (this.CreatedBy == originalCreatedByValue && this.CreatedBy != dbCreatedByValue) 
            { 
                this.CreatedBy = dbCreatedByValue; 
            }

            if (this.ModifiedBy != originalModifiedByValue) 
            { 
                databaseItem["Editor"] = this.ModifiedBy; 
            } 
            else if (this.ModifiedBy == originalModifiedByValue && this.ModifiedBy != dbModifiedByValue) 
            { 
                this.ModifiedBy = dbModifiedByValue; 
            } 
        } 
    }

    public DateTime Modified { get; set; } 
    public DateTime Created { get; set; } 
    public string CreatedBy { get; set; } 
    public string ModifiedBy { get; set; } 
}
share|improve this answer
    
we excluded all of the usage of SPMetal in our team, so did some other developers I have worked with. SPMetal is very very unereliable, have a lot of limitations, sometimes hard to work around. So we simply rewrote relevant parts of it to remove the technology risk. Honestly, if you compare the number of lines you wrote to workaround to the simpler code that use direct list item get/set value, it's hardly in favor of SPMetal. I won't be able to test your scenario, as I don't work with SPmetal anymore, so I'll have to trust you! –  Steve B Apr 22 '13 at 7:59
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.