SQL Server guide

Add link to existing work item (attachment as reference)


Add an attachment reference to an existing work item using a JSON PATCH body. Use the attachment URL you received from the upload endpoint (or any existing attachment URL). The example adds a history comment and one AttachedFile relation; replace YOUR_ORGANIZATION, YOUR_PROJECT, WORK_ITEM_NUMBER, and ATTACHMENT_ID with your values. Useful for linking screenshots or documents to a work item after upload.

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.

SELECT *
FROM generic_request
WITH(
	  Url='YOUR_ORGANIZATION/YOUR_PROJECT/_apis/wit/workitems/WORK_ITEM_NUMBER?api-version=7.0',
	  RequestMethod='PATCH',
	  Headers='Content-Type: application/json-patch+json',
	  Meta='id:int; fields.System.Title:string; fields.System.State:string; fields.System.WorkItemType:string; relations:string(4000)',
	  Body='
[
  {
    "op": "add",
    "path": "/fields/System.History",
    "value": "Adding the necessary spec"
  },
  {
    "op": "add",
    "path": "/relations/-",
    "value": {
      "rel": "AttachedFile",
      "url": "https://dev.azure.com/YOUR_ORGANIZATION/YOUR_PROJECT/_apis/wit/attachments/ATTACHMENT_ID?fileName=Screenshot.png",
      "attributes": {
        "comment": "Error screenshot"
      }
    }
  }
]
'
)

Using OPENQUERY in SQL Server

SELECT * FROM OPENQUERY([LS_TO_AZURE_DEVOPS_IN_GATEWAY], 'SELECT *
FROM generic_request
WITH(
	  Url=''YOUR_ORGANIZATION/YOUR_PROJECT/_apis/wit/workitems/WORK_ITEM_NUMBER?api-version=7.0'',
	  RequestMethod=''PATCH'',
	  Headers=''Content-Type: application/json-patch+json'',
	  Meta=''id:int; fields.System.Title:string; fields.System.State:string; fields.System.WorkItemType:string; relations:string(4000)'',
	  Body=''
[
  {
    "op": "add",
    "path": "/fields/System.History",
    "value": "Adding the necessary spec"
  },
  {
    "op": "add",
    "path": "/relations/-",
    "value": {
      "rel": "AttachedFile",
      "url": "https://dev.azure.com/YOUR_ORGANIZATION/YOUR_PROJECT/_apis/wit/attachments/ATTACHMENT_ID?fileName=Screenshot.png",
      "attributes": {
        "comment": "Error screenshot"
      }
    }
  }
]
''
)')

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_AZURE_DEVOPS_IN_GATEWAY] syntax.

DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT *
FROM generic_request
WITH(
	  Url=''YOUR_ORGANIZATION/YOUR_PROJECT/_apis/wit/workitems/WORK_ITEM_NUMBER?api-version=7.0'',
	  RequestMethod=''PATCH'',
	  Headers=''Content-Type: application/json-patch+json'',
	  Meta=''id:int; fields.System.Title:string; fields.System.State:string; fields.System.WorkItemType:string; relations:string(4000)'',
	  Body=''
[
  {
    "op": "add",
    "path": "/fields/System.History",
    "value": "Adding the necessary spec"
  },
  {
    "op": "add",
    "path": "/relations/-",
    "value": {
      "rel": "AttachedFile",
      "url": "https://dev.azure.com/YOUR_ORGANIZATION/YOUR_PROJECT/_apis/wit/attachments/ATTACHMENT_ID?fileName=Screenshot.png",
      "attributes": {
        "comment": "Error screenshot"
      }
    }
  }
]
''
)'
EXEC (@MyQuery) AT [LS_TO_AZURE_DEVOPS_IN_GATEWAY]