Domanda

Ho la lista di SharePoint "Eventi" con il campo Posizione .

Sto cercando di aggiungere dal campo Web esistente con stesso nome interno da altro pacchetto.

    var fields = contentType.ParentWeb.Fields;

 SPField fieldToAdd = null;
                foreach (SPField field in fields)
                {
                    if (field.Id == fieldId)
                    {
                        fieldToAdd = field;
                        break;
                    }
                }

 contentType.FieldLinks.Add(new SPFieldLink(fieldToAdd));
.

E ho ottenuto un'eccezione:

A duplicate field name "Location" was found.
.

Quando sto aggiungendo questi campi in SharePoint UI, verrà modificato in Location0 e verrà aggiunto con successo!

Come fare in realtà lo stesso nel codice?Voglio aggiungerli entrambi e è accettabile cambiare un nome interno in posizione0.

È stato utile?

Soluzione

Did not get your code clearly, what is fieldId and how are you getting this fieldId? my guess is location is already there in the content type you can not add the same field again, create a new field if you wish, in that case your code will be (after foreach) -

string fldName = fields.Add(fieldToAdd.Title, fieldToAdd.Type, fieldToAdd.Required);
SPField newField = fields.GetField(fldName);
contentType.FieldLinks.Add(new SPFieldLink(newField));

In this case a new field is created with same display name and same type as fieldToAdd, but with different internal name (you dont have to worry about what the internal name is, system will determine) and add that field to your content type

P.S. - you should consider using break; after fieldToAdd = field;

:)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange
scroll top