I am trying to copy an existing SharePoint List using SharePoint REST API. I was able to achieve what I wanted to do, but the display names for the fields and the list itself is not displayed within the view. However, when I get field or list information, the internalName and the displayName is updated. What am I doing wrong?
To copy a list, I am doing the following steps:
- Get field information from existing list
GET
let requestUrl = "/_api/web/lists/getbytitle('<ListTitle>')/Fields?$filter=Hidden eq false and ReadOnlyField eq false and InternalName ne 'Attachments' and InternalName ne 'ContentType'"
- Create list using internalName
POST
let requestUrl = "/_api/web/lists"
let body: [String: Any] = [
"__metadata": ["type": "SP.List"],
"BaseTemplate": 100,
"Title": "<ListInternalName>"
]
- Update new list name
MERGE
let requestUrl = "/_api/web/lists(guid'<NewListId>')"
let body: [String: Any] = [
"__metadata": ["type": "SP.List"],
"Title": "<ListDisplayName>",
]
- Add fields to new list using data from step 1
POST
let requestUrl = "/_api/web/lists(guid'\(listInfo.listsId)')/Fields"
let body: [String: Any] = [
"__metadata": ["type": "SP.Field"],
"Title": <FieldInternalName>,
"FieldTypeKind": <FieldTypeKind>,
]
- Update new field name
MERGE
let requestUrl = "/_api/web/lists(guid'<NewListId>')/Fields('<NewFieldId>')"
let body: [String: Any] = [
"__metadata": ["type": "SP.Field"],
"Title": "<NewFieldDisplayName>"
]
- Add fields to default view("All Items")
POST
let requestUrl = "/_api/web/lists(guid'<NewListId>')/views(guid'<ViewId>')/ViewFields/AddViewField('<NewFieldInternalName>')"