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 have created a list using schema.xml and then i have created a list instance. later on i need to to add one column in the list schema which is not shown in the existing list instance. any idea how to resolve it?

share|improve this question
add comment

1 Answer

The list schema is actually a template used at creation time.

If you want to update the schema of existing list, you will have to update the list itself (or the content type if the list is bound to a site content type).

There is no OOB way to do this, but here is a script I use in an upgrade process. My app use site content types, and list schema attached to this content type. The feature upgrade add the column in the xml declaration for new list, and the PS script add the column to existing list :

add-pssnapin Microsoft.SharePoint.PowerShell -EA 0

function AddFieldToContentType([Microsoft.SharePoint.SPWeb] $web, [string]$contentTypeName, [string]$xmlSchema)
{
    $xml = [xml]$xmlSchema
    $fieldName = $xml.Field.Name
    if ($web.Fields.ContainsField($fieldName) -Eq $false) {
        $fieldName = $web.Fields.AddFieldAsXml($xmlSchema)
        $web.Update()
    }
    $field = $web.Fields.GetFieldByInternalName($fieldName)

    $ct = $web.ContentTypes[$contentTypeName]
    if($ct.FieldLinks[$fieldName] -Eq $null) {
        $link = new-object Microsoft.SharePoint.SPFieldLink $field
        $ct.FieldLinks.Add($link)
        $ct.Update($true)
    }

}
$url="http://server/sites/sitecoll"

[Microsoft.SharePoint.SPWeb]$web = get-spweb -Identity $url
$xml = '
<Field
    Type="User"
    Indexed="TRUE"
    DisplayName="New column"
    List="UserInfo" 
    Group="Your group" 
    ShowField="ImnName" 
    UserSelectionMode="PeopleAndGroups" 
    UserSelectionScope="0" 
    Mult="False" 
    Sortable="FALSE" 
    ID="{373FAFE5-45F2-455B-8153-89C7B48D4F7A}" 
    Name="NewColumn" />'

AddFieldToContentType $web "YourContentType" $xml

[Edit] You can reuse the same principle in a feature receiver which upgrades the lists...

share|improve this answer
    
Thanks a lot Steve! –  Zeeshan Jul 19 '11 at 7:38
    
I'd also like to add, that the xml declaration in the script should match exactly the xml declaration of the feature's xml. This is not technically required, but will prevent some headache when maintening the application. –  Steve B Jul 19 '11 at 8:00
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.