SQL Server guide

Create a list item with multi-select choice fields


Creates a list item with multi-select Choice fields. Requires passing values as a JSON array and specifying the OData type (e.g. Collection(Edm.String)).

Standard SQL query example

This is the base query accepted by the connector. To execute it in SQL Server, you have to pass it to the Data Gateway via a Linked Server. See how to accomplish this using the examples below.

--new version --				
INSERT INTO MyList(Title, MultiChoiceField, MultiChoiceFieldODataType)
VALUES ('John Doe', '["Choice1","Choice5"]', 'Collection(Edm.String)')
WITH (Output=1)

--old version --				

INSERT INTO MyList(Title, MultiChoiceField, "fields.MultiChoiceField@odata\u002Etype")
VALUES ('John Doe', '["Choice1","Choice5"]', 'Collection(Edm.String)')
WITH (Output=1)

--Considering "MultiChoiceField" is the name of the field in SharePoint List

Using OPENQUERY in SQL Server

SELECT * FROM OPENQUERY([LS_TO_SHAREPOINT_ONLINE_IN_GATEWAY], '--new version --				
INSERT INTO MyList(Title, MultiChoiceField, MultiChoiceFieldODataType)
VALUES (''John Doe'', ''["Choice1","Choice5"]'', ''Collection(Edm.String)'')
WITH (Output=1)

--old version --				

INSERT INTO MyList(Title, MultiChoiceField, "fields.MultiChoiceField@odata\u002Etype")
VALUES (''John Doe'', ''["Choice1","Choice5"]'', ''Collection(Edm.String)'')
WITH (Output=1)

--Considering "MultiChoiceField" is the name of the field in SharePoint List')

Using EXEC in SQL Server (handling larger SQL text)

The major drawback of OPENQUERY is its inability to incorporate variables within SQL statements. This often leads to the use of cumbersome dynamic SQL (with numerous ticks and escape characters).

Fortunately, starting with SQL 2005 and onwards, you can utilize the EXEC (your_sql) AT [LS_TO_SHAREPOINT_ONLINE_IN_GATEWAY] syntax.

DECLARE @MyQuery NVARCHAR(MAX) = '--new version --				
INSERT INTO MyList(Title, MultiChoiceField, MultiChoiceFieldODataType)
VALUES (''John Doe'', ''["Choice1","Choice5"]'', ''Collection(Edm.String)'')
WITH (Output=1)

--old version --				

INSERT INTO MyList(Title, MultiChoiceField, "fields.MultiChoiceField@odata\u002Etype")
VALUES (''John Doe'', ''["Choice1","Choice5"]'', ''Collection(Edm.String)'')
WITH (Output=1)

--Considering "MultiChoiceField" is the name of the field in SharePoint List'
EXEC (@MyQuery) AT [LS_TO_SHAREPOINT_ONLINE_IN_GATEWAY]