assets log {ts '2026-04-16 09:21:08'} SQL declare @ownerId int = 207 ; declare @ownerType varchar(3) = 'MFR' ; declare @contentPrefix varchar(128) = 'http://dev.fsenablers.com/FSLContent/' ; declare @todayDate datetime = ?; with docCategories as ( SELECT cats.attrID as categoryId, cats.attrDescription AS categoryName FROM tbl_ORG_Attributes cats with (nolock) WHERE cats.OwnerID = @ownerId AND cats.FSL_TableCode = @ownerType AND cats.attrTypeID = 31 ), rawAssets as ( select null contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, cast( null as varchar ) docTitle, cast( null as int) as categoryId, cast( null as int ) contentId, cast( null as int) fileId, 0.00 fileSize, null fileExt, null docDescription, cast( null as datetime ) publishDate, cast( null as datetime ) unpublishDate, 'N' isPublished, cast( null as datetime ) uploadDate, cast( null as datetime ) updateDate, cast( null as int ) updateUserId, 0 restricted, -1 thumbnailId, -1 highresCntId, 99 as sortRank , 0 as freshness , 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr, cast( null as varchar ) as assetSelectionKey, 0 as selectedRecord , 'N' as internalOnly, '' as recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds union all select 'DOC' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, d.docTitle, d.repCategoryId as categoryId, d.contentId, d.docId as fileId, CAST((d.fileSize/1024) AS float) as fileSize , d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, case when d.publishDate is not null and d.publishDate > @todayDate then 'F' when d.publishDate is not null and d.unpublishDate is not null and @todayDate >= d.publishDate and @todayDate <= d.unpublishDate then 'Y' when d.publishDate is not null and d.unpublishDate is null and d.publishDate <= @todayDate then 'Y' when d.publishDate is null then 'N' else 'N' end isPublished, d.uploadDate, d.lastUpdatedDate updateDate, d.lastUpdatedBy updateUserId, 0 restricted, thumbnailId, -1 highresCntId, sortRank , coalesce(dateDiff(day, d.lastUpdatedDate, @todayDate),0) as freshness, case when d.privateYesNo = 1 then 'Y' else 'N' end privateYesNo, ou.firstname + ' ' + ou.lastname as uploadedByFullName, d.keyWords, d.unpublishToDo, d.internalNotes , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END), ',') AS linked_opr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END), ',') AS linked_prd , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END), ',') AS linked_sku , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END), ',') AS linked_cdr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END), ',') AS linked_mfr , cast(concat('DOC',':',d.docId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , d.internalOnly, d.recordSource , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 23 THEN li.child_attrID ELSE NULL END), ',') AS permissionGrpIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 7 THEN li.child_attrID ELSE NULL END), ',') AS linked_brokerIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 31 THEN li.child_attrID ELSE NULL END), ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = d.docId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 34 THEN li.child_attrID ELSE NULL END), ',') AS linked_UserMemberIds from tbl_DOC_Documents d with (nolock) LEFT OUTER JOIN tbl_FSPRO_Members ou with (nolock) ON d.AuthorUserId = ou.fspro_userId left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = d.docId and li.child_attrTypeID in (41,59,60,63,42,23,7,31,34) and li.mstr_attrTypeID = 30 left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('DOC',':',d.docId) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType GROUP BY d.docTitle, d.repCategoryId, d.contentId, d.docId,d.fileSize, d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, d.uploadDate, d.lastUpdatedDate, d.lastUpdatedBy,thumbnailId, sortRank , d.privateYesNo , ou.firstname,ou.lastname, d.keyWords, d.unpublishToDo, d.internalNotes,ast.assetSelectionKey, d.internalOnly, d.recordSource union all select 'IMG' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, i.imgTitle as docTitle, i.categoryId, i.contentId, i.imgId as fileId, CAST((i.fileSize/1024) AS float) as fileSize, i.fileExt, i.imgDescription as docDescription, i.publishDate, i.unpublishDate, case when i.publishDate is not null and i.publishDate > @todayDate then 'F' when i.publishDate is not null and i.unpublishDate is not null and @todayDate >= i.publishDate and @todayDate <= i.unpublishDate then 'Y' when i.publishDate is not null and i.unpublishDate is null and i.publishDate <= @todayDate then 'Y' when i.publishDate is null then 'N' else 'N' end isPublished, i.uploadDate, i.lastUpdatedDate updateDate, i.lastUpdatedBy updateUserId, 0 restricted, -1 thumbnailId, i.highresCntId, i.sortRank, coalesce(dateDiff(day, lastUpdatedDate, @todayDate),0) as freshness, case when i.privateYesNo = 1 then 'Y' else 'N' end privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , STRING_AGG(CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END, ',') AS linked_opr , STRING_AGG(CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END, ',') AS linked_prd , STRING_AGG(CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END, ',') AS linked_sku , STRING_AGG(CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END, ',') AS linked_cdr , STRING_AGG(CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END, ',') AS linked_mfr , cast(concat('IMG',':',i.imgId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , i.internalOnly, i.recordSource , STRING_AGG(CASE WHEN c.child_attrTypeID = 23 THEN c.child_attrID ELSE NULL END, ',') AS permissionGrpIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 7 THEN c.child_attrID ELSE NULL END, ',') AS linked_brokerIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 31 THEN c.child_attrID ELSE NULL END, ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = i.imgId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CASE WHEN c.child_attrTypeID = 34 THEN c.child_attrID ELSE NULL END, ',') AS linked_UserMemberIds from tbl_IMG_Images i with (nolock) left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = i.imgId and li.child_attrTypeID in (41,59,60,63,42) and li.mstr_attrTypeID = 33 left outer JOIN tbl_ORG_AttributeLinks c ON c.mstr_attrID = i.imgId and c.child_attrTypeID in (23,7,31,34) and c.mstr_attrTypeID = 30 left outer join tbl_DOC_Categories dc with (nolock) ON c.child_attrTypeID = 31 AND dc.categoryId = c.child_attrID AND dc.ownerId = @ownerId AND dc.fsl_tablecode = @ownerType left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('IMG',':',i.imgId) where i.ownerId = @ownerId and i.fsl_tablecode = @ownerType GROUP BY i.imgTitle, i.CategoryID, i.contentId, i.imgId, i.fileSize, i.fileExt, i.imgDescription, i.publishDate, i.unpublishDate, i.uploadDate, i.lastUpdatedDate, i.lastUpdatedBy, i.highresCntId, i.sortRank, i.privateYesNo,ast.assetSelectionKey, i.internalOnly, i.recordSource ), filteredRawAssets as ( select ra.* from rawAssets ra where (1=1 or ra.linked_brokerIds in (?) ) ) , assetsWithContentURL1 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL, c.fileName as docFileName, lub.firstname + ' ' + lub.lastname as lastUpdatedByFullName from filteredRawAssets ra LEFT OUTER JOIN tbl_FSPRO_Members lub with (nolock) ON ra.updateUserId = lub.fspro_userId inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) , assetsWithContentURL2 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as thumbnailURL, c.fileName as thnFileName from assetsWithContentURL1 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.thumbnailId and c.contentId != 755 ) , assetsWithContentURL3 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as highresURL, c.fileName as highresCntFileName from assetsWithContentURL2 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.highresCntId and c.contentId != 755 ) , assets as ( select a.*, case when right( a.contentURL, charindex( '.', reverse( a.contentURL )) - 1 ) in ( 'BMP','GIF','JPEG','JPG','PNG','TIF','TIFF','WBMP' ) then left( a.contentURL, len( a.contentURL ) - charindex( '.', reverse( a.contentURL ) )) + '_50' + right( a.contentURL, charindex( '.', reverse( a.contentURL )) ) else null end previewURL from assetsWithContentURL3 a ) , categories as ( select 'OPR' contentDomain, categoryId, categoryDescription from tbl_OPR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'CDR' contentDomain, categoryId, categoryDescription from tbl_CDR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'BPL' contentDomain, categoryId, categoryDescription from tbl_BPL_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'DOC' contentDomain, categoryId, categoryName as categoryDescription from tbl_DOC_Categories with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType ) , assetsWithCategory as ( select a.*, c.categoryDescription from assets a left outer join categories c on c.contentDomain = a.contentDomain and c.categoryId = a.categoryId ) ,industryCategories as ( SELECT categoryId, categoryDescription FROM tbl_PRD_RepositoryCategories with (nolock) WHERE containerType = 'SKU' AND ownerID = 0 AND ownerType = '*' AND left(categoryDescription, 3) <> 'GS1' ) , assetsWithIndustryCategory as ( select a.*, ic.categoryDescription as industryCategoryDescription from assetsWithCategory a left outer join industryCategories ic on ic.categoryId = a.categoryId ) , assetsWithPartner as ( select a.*, opr.companyName as partnerName, a.containerId as partnerId, a.containerType as partnerType, opr.territoryId from assetsWithIndustryCategory a inner join tbl_OPR_CLientOperators opr with (nolock) on opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = a.containerId where a.containerType = 'OPR' union all select a.*, cdr.cdr_dstName as partnerName, a.containerId as partnerId, a.containerType as partnerType, cdr.cdr_territoryId as territoryId from assetsWithIndustryCategory a inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = @ownerId and cdr.fsl_tablecode = @ownerType and cdr.cdr_recordId = a.containerId where a.containerType = 'CDR' union all select a.*, mfr.mfr_name as partnerName, mfr.mfr_id as partnerId, 'MFR' as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a inner join tbl_BRO_Principals bpl with (nolock) on bpl.ownerId = @ownerId and bpl.ownerType = @ownerType and bpl.principalId = a.containerId inner join tblManufacturers mfr with (nolock) on mfr.mfr_id = bpl.mfrId where a.containerType = 'BPL' union all select a.*,cast (null as varchar ) as partnerName, cast( null as integer ) as partnerId, cast( null as varchar ) as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a where coalesce( a.containerType, '???') not in ( 'BPL', 'OPR', 'CDR' ) ), assetsWithUser as ( select a.*, m.email updateUserEmail, m.firstName + ' ' + m.lastName as updateUserFullName, case when len(isnull(a.linked_prd,'')) > 0 or len(isnull(a.linked_sku,'')) > 0 then 'Y' else 'N' end as prdLink, case when len(isnull(a.linked_opr,'')) > 0 then 'Y' else 'N' end as oprLink, case when len(isnull(a.linked_cdr,'')) > 0 then 'Y' else 'N' end as cdrLink, case when len(isnull(a.linked_mfr,'')) > 0 then 'Y' else 'N' end as mfrLink from assetsWithPartner as a left outer join tbl_Fspro_members m with (nolock) on m.ownerId > 0 and m.fspro_userId = a.updateUserId ) , assetsWithUser_mappedData as ( select * from assetsWithUser ) , assetsWithUser_preData as ( select * from assetsWithUser_mappedData with (nolock) where ( ( 1 = 1 ) and ( ( ( [isPublished] = 'Y' ) or ( [isPublished] = 'F' ) ) ) ) ) , assetsWithUser_data as ( select * from assetsWithUser_preData ) , assetsWithUser_return as ( select * from assetsWithUser_data where 1 = 1 order by docTitle offset 0 rows fetch next 50 rows only ) , assetsWithUser_result as ( select ( select count(*) from assetsWithUser_data ) as totalCount, ( select * from assetsWithUser_return for json auto, include_null_values ) as data ) select * from assetsWithUser_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER -- 2026-04-16 09:21:07.758 -- 0 {ts '2026-04-16 12:55:18'} SQL declare @ownerId int = 207 ; declare @ownerType varchar(3) = 'MFR' ; declare @contentPrefix varchar(128) = 'http://dev.fsenablers.com/FSLContent/' ; declare @todayDate datetime = ?; with docCategories as ( SELECT cats.attrID as categoryId, cats.attrDescription AS categoryName FROM tbl_ORG_Attributes cats with (nolock) WHERE cats.OwnerID = @ownerId AND cats.FSL_TableCode = @ownerType AND cats.attrTypeID = 31 ), rawAssets as ( select null contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, cast( null as varchar ) docTitle, cast( null as int) as categoryId, cast( null as int ) contentId, cast( null as int) fileId, 0.00 fileSize, null fileExt, null docDescription, cast( null as datetime ) publishDate, cast( null as datetime ) unpublishDate, 'N' isPublished, cast( null as datetime ) uploadDate, cast( null as datetime ) updateDate, cast( null as int ) updateUserId, 0 restricted, -1 thumbnailId, -1 highresCntId, 99 as sortRank , 0 as freshness , 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr, cast( null as varchar ) as assetSelectionKey, 0 as selectedRecord , 'N' as internalOnly, '' as recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds union all select 'DOC' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, d.docTitle, d.repCategoryId as categoryId, d.contentId, d.docId as fileId, CAST((d.fileSize/1024) AS float) as fileSize , d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, case when d.publishDate is not null and d.publishDate > @todayDate then 'F' when d.publishDate is not null and d.unpublishDate is not null and @todayDate >= d.publishDate and @todayDate <= d.unpublishDate then 'Y' when d.publishDate is not null and d.unpublishDate is null and d.publishDate <= @todayDate then 'Y' when d.publishDate is null then 'N' else 'N' end isPublished, d.uploadDate, d.lastUpdatedDate updateDate, d.lastUpdatedBy updateUserId, 0 restricted, thumbnailId, -1 highresCntId, sortRank , coalesce(dateDiff(day, d.lastUpdatedDate, @todayDate),0) as freshness, case when d.privateYesNo = 1 then 'Y' else 'N' end privateYesNo, ou.firstname + ' ' + ou.lastname as uploadedByFullName, d.keyWords, d.unpublishToDo, d.internalNotes , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END), ',') AS linked_opr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END), ',') AS linked_prd , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END), ',') AS linked_sku , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END), ',') AS linked_cdr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END), ',') AS linked_mfr , cast(concat('DOC',':',d.docId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , d.internalOnly, d.recordSource , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 23 THEN li.child_attrID ELSE NULL END), ',') AS permissionGrpIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 7 THEN li.child_attrID ELSE NULL END), ',') AS linked_brokerIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 31 THEN li.child_attrID ELSE NULL END), ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = d.docId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 34 THEN li.child_attrID ELSE NULL END), ',') AS linked_UserMemberIds from tbl_DOC_Documents d with (nolock) LEFT OUTER JOIN tbl_FSPRO_Members ou with (nolock) ON d.AuthorUserId = ou.fspro_userId left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = d.docId and li.child_attrTypeID in (41,59,60,63,42,23,7,31,34) and li.mstr_attrTypeID = 30 left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('DOC',':',d.docId) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType GROUP BY d.docTitle, d.repCategoryId, d.contentId, d.docId,d.fileSize, d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, d.uploadDate, d.lastUpdatedDate, d.lastUpdatedBy,thumbnailId, sortRank , d.privateYesNo , ou.firstname,ou.lastname, d.keyWords, d.unpublishToDo, d.internalNotes,ast.assetSelectionKey, d.internalOnly, d.recordSource union all select 'IMG' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, i.imgTitle as docTitle, i.categoryId, i.contentId, i.imgId as fileId, CAST((i.fileSize/1024) AS float) as fileSize, i.fileExt, i.imgDescription as docDescription, i.publishDate, i.unpublishDate, case when i.publishDate is not null and i.publishDate > @todayDate then 'F' when i.publishDate is not null and i.unpublishDate is not null and @todayDate >= i.publishDate and @todayDate <= i.unpublishDate then 'Y' when i.publishDate is not null and i.unpublishDate is null and i.publishDate <= @todayDate then 'Y' when i.publishDate is null then 'N' else 'N' end isPublished, i.uploadDate, i.lastUpdatedDate updateDate, i.lastUpdatedBy updateUserId, 0 restricted, -1 thumbnailId, i.highresCntId, i.sortRank, coalesce(dateDiff(day, lastUpdatedDate, @todayDate),0) as freshness, case when i.privateYesNo = 1 then 'Y' else 'N' end privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , STRING_AGG(CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END, ',') AS linked_opr , STRING_AGG(CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END, ',') AS linked_prd , STRING_AGG(CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END, ',') AS linked_sku , STRING_AGG(CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END, ',') AS linked_cdr , STRING_AGG(CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END, ',') AS linked_mfr , cast(concat('IMG',':',i.imgId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , i.internalOnly, i.recordSource , STRING_AGG(CASE WHEN c.child_attrTypeID = 23 THEN c.child_attrID ELSE NULL END, ',') AS permissionGrpIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 7 THEN c.child_attrID ELSE NULL END, ',') AS linked_brokerIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 31 THEN c.child_attrID ELSE NULL END, ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = i.imgId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CASE WHEN c.child_attrTypeID = 34 THEN c.child_attrID ELSE NULL END, ',') AS linked_UserMemberIds from tbl_IMG_Images i with (nolock) left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = i.imgId and li.child_attrTypeID in (41,59,60,63,42) and li.mstr_attrTypeID = 33 left outer JOIN tbl_ORG_AttributeLinks c ON c.mstr_attrID = i.imgId and c.child_attrTypeID in (23,7,31,34) and c.mstr_attrTypeID = 30 left outer join tbl_DOC_Categories dc with (nolock) ON c.child_attrTypeID = 31 AND dc.categoryId = c.child_attrID AND dc.ownerId = @ownerId AND dc.fsl_tablecode = @ownerType left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('IMG',':',i.imgId) where i.ownerId = @ownerId and i.fsl_tablecode = @ownerType GROUP BY i.imgTitle, i.CategoryID, i.contentId, i.imgId, i.fileSize, i.fileExt, i.imgDescription, i.publishDate, i.unpublishDate, i.uploadDate, i.lastUpdatedDate, i.lastUpdatedBy, i.highresCntId, i.sortRank, i.privateYesNo,ast.assetSelectionKey, i.internalOnly, i.recordSource ), filteredRawAssets as ( select ra.* from rawAssets ra where (1=1 or ra.linked_brokerIds in (?) ) ) , assetsWithContentURL1 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL, c.fileName as docFileName, lub.firstname + ' ' + lub.lastname as lastUpdatedByFullName from filteredRawAssets ra LEFT OUTER JOIN tbl_FSPRO_Members lub with (nolock) ON ra.updateUserId = lub.fspro_userId inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) , assetsWithContentURL2 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as thumbnailURL, c.fileName as thnFileName from assetsWithContentURL1 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.thumbnailId and c.contentId != 755 ) , assetsWithContentURL3 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as highresURL, c.fileName as highresCntFileName from assetsWithContentURL2 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.highresCntId and c.contentId != 755 ) , assets as ( select a.*, case when right( a.contentURL, charindex( '.', reverse( a.contentURL )) - 1 ) in ( 'BMP','GIF','JPEG','JPG','PNG','TIF','TIFF','WBMP' ) then left( a.contentURL, len( a.contentURL ) - charindex( '.', reverse( a.contentURL ) )) + '_50' + right( a.contentURL, charindex( '.', reverse( a.contentURL )) ) else null end previewURL from assetsWithContentURL3 a ) , categories as ( select 'OPR' contentDomain, categoryId, categoryDescription from tbl_OPR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'CDR' contentDomain, categoryId, categoryDescription from tbl_CDR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'BPL' contentDomain, categoryId, categoryDescription from tbl_BPL_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'DOC' contentDomain, categoryId, categoryName as categoryDescription from tbl_DOC_Categories with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType ) , assetsWithCategory as ( select a.*, c.categoryDescription from assets a left outer join categories c on c.contentDomain = a.contentDomain and c.categoryId = a.categoryId ) ,industryCategories as ( SELECT categoryId, categoryDescription FROM tbl_PRD_RepositoryCategories with (nolock) WHERE containerType = 'SKU' AND ownerID = 0 AND ownerType = '*' AND left(categoryDescription, 3) <> 'GS1' ) , assetsWithIndustryCategory as ( select a.*, ic.categoryDescription as industryCategoryDescription from assetsWithCategory a left outer join industryCategories ic on ic.categoryId = a.categoryId ) , assetsWithPartner as ( select a.*, opr.companyName as partnerName, a.containerId as partnerId, a.containerType as partnerType, opr.territoryId from assetsWithIndustryCategory a inner join tbl_OPR_CLientOperators opr with (nolock) on opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = a.containerId where a.containerType = 'OPR' union all select a.*, cdr.cdr_dstName as partnerName, a.containerId as partnerId, a.containerType as partnerType, cdr.cdr_territoryId as territoryId from assetsWithIndustryCategory a inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = @ownerId and cdr.fsl_tablecode = @ownerType and cdr.cdr_recordId = a.containerId where a.containerType = 'CDR' union all select a.*, mfr.mfr_name as partnerName, mfr.mfr_id as partnerId, 'MFR' as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a inner join tbl_BRO_Principals bpl with (nolock) on bpl.ownerId = @ownerId and bpl.ownerType = @ownerType and bpl.principalId = a.containerId inner join tblManufacturers mfr with (nolock) on mfr.mfr_id = bpl.mfrId where a.containerType = 'BPL' union all select a.*,cast (null as varchar ) as partnerName, cast( null as integer ) as partnerId, cast( null as varchar ) as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a where coalesce( a.containerType, '???') not in ( 'BPL', 'OPR', 'CDR' ) ), assetsWithUser as ( select a.*, m.email updateUserEmail, m.firstName + ' ' + m.lastName as updateUserFullName, case when len(isnull(a.linked_prd,'')) > 0 or len(isnull(a.linked_sku,'')) > 0 then 'Y' else 'N' end as prdLink, case when len(isnull(a.linked_opr,'')) > 0 then 'Y' else 'N' end as oprLink, case when len(isnull(a.linked_cdr,'')) > 0 then 'Y' else 'N' end as cdrLink, case when len(isnull(a.linked_mfr,'')) > 0 then 'Y' else 'N' end as mfrLink from assetsWithPartner as a left outer join tbl_Fspro_members m with (nolock) on m.ownerId > 0 and m.fspro_userId = a.updateUserId ) , assetsWithUser_mappedData as ( select * from assetsWithUser ) , assetsWithUser_preData as ( select * from assetsWithUser_mappedData with (nolock) where ( ( 1 = 1 ) and ( ( ( [isPublished] = 'Y' ) or ( [isPublished] = 'F' ) ) ) ) ) , assetsWithUser_data as ( select * from assetsWithUser_preData ) , assetsWithUser_return as ( select * from assetsWithUser_data where 1 = 1 order by docTitle offset 0 rows fetch next 50 rows only ) , assetsWithUser_result as ( select ( select count(*) from assetsWithUser_data ) as totalCount, ( select * from assetsWithUser_return for json auto, include_null_values ) as data ) select * from assetsWithUser_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER -- 2026-04-16 12:55:18.167 -- 0 {ts '2026-04-16 12:55:26'} SQL declare @ownerId int = 207 ; declare @ownerType varchar(3) = 'MFR' ; declare @contentPrefix varchar(128) = 'http://dev.fsenablers.com/FSLContent/' ; declare @todayDate datetime = ?; with docCategories as ( SELECT cats.attrID as categoryId, cats.attrDescription AS categoryName FROM tbl_ORG_Attributes cats with (nolock) WHERE cats.OwnerID = @ownerId AND cats.FSL_TableCode = @ownerType AND cats.attrTypeID = 31 ), rawAssets as ( select null contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, cast( null as varchar ) docTitle, cast( null as int) as categoryId, cast( null as int ) contentId, cast( null as int) fileId, 0.00 fileSize, null fileExt, null docDescription, cast( null as datetime ) publishDate, cast( null as datetime ) unpublishDate, 'N' isPublished, cast( null as datetime ) uploadDate, cast( null as datetime ) updateDate, cast( null as int ) updateUserId, 0 restricted, -1 thumbnailId, -1 highresCntId, 99 as sortRank , 0 as freshness , 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr, cast( null as varchar ) as assetSelectionKey, 0 as selectedRecord , 'N' as internalOnly, '' as recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds union all select 'DOC' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, d.docTitle, d.repCategoryId as categoryId, d.contentId, d.docId as fileId, CAST((d.fileSize/1024) AS float) as fileSize , d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, case when d.publishDate is not null and d.publishDate > @todayDate then 'F' when d.publishDate is not null and d.unpublishDate is not null and @todayDate >= d.publishDate and @todayDate <= d.unpublishDate then 'Y' when d.publishDate is not null and d.unpublishDate is null and d.publishDate <= @todayDate then 'Y' when d.publishDate is null then 'N' else 'N' end isPublished, d.uploadDate, d.lastUpdatedDate updateDate, d.lastUpdatedBy updateUserId, 0 restricted, thumbnailId, -1 highresCntId, sortRank , coalesce(dateDiff(day, d.lastUpdatedDate, @todayDate),0) as freshness, case when d.privateYesNo = 1 then 'Y' else 'N' end privateYesNo, ou.firstname + ' ' + ou.lastname as uploadedByFullName, d.keyWords, d.unpublishToDo, d.internalNotes , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END), ',') AS linked_opr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END), ',') AS linked_prd , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END), ',') AS linked_sku , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END), ',') AS linked_cdr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END), ',') AS linked_mfr , cast(concat('DOC',':',d.docId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , d.internalOnly, d.recordSource , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 23 THEN li.child_attrID ELSE NULL END), ',') AS permissionGrpIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 7 THEN li.child_attrID ELSE NULL END), ',') AS linked_brokerIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 31 THEN li.child_attrID ELSE NULL END), ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = d.docId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 34 THEN li.child_attrID ELSE NULL END), ',') AS linked_UserMemberIds from tbl_DOC_Documents d with (nolock) LEFT OUTER JOIN tbl_FSPRO_Members ou with (nolock) ON d.AuthorUserId = ou.fspro_userId left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = d.docId and li.child_attrTypeID in (41,59,60,63,42,23,7,31,34) and li.mstr_attrTypeID = 30 left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('DOC',':',d.docId) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType GROUP BY d.docTitle, d.repCategoryId, d.contentId, d.docId,d.fileSize, d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, d.uploadDate, d.lastUpdatedDate, d.lastUpdatedBy,thumbnailId, sortRank , d.privateYesNo , ou.firstname,ou.lastname, d.keyWords, d.unpublishToDo, d.internalNotes,ast.assetSelectionKey, d.internalOnly, d.recordSource union all select 'IMG' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, i.imgTitle as docTitle, i.categoryId, i.contentId, i.imgId as fileId, CAST((i.fileSize/1024) AS float) as fileSize, i.fileExt, i.imgDescription as docDescription, i.publishDate, i.unpublishDate, case when i.publishDate is not null and i.publishDate > @todayDate then 'F' when i.publishDate is not null and i.unpublishDate is not null and @todayDate >= i.publishDate and @todayDate <= i.unpublishDate then 'Y' when i.publishDate is not null and i.unpublishDate is null and i.publishDate <= @todayDate then 'Y' when i.publishDate is null then 'N' else 'N' end isPublished, i.uploadDate, i.lastUpdatedDate updateDate, i.lastUpdatedBy updateUserId, 0 restricted, -1 thumbnailId, i.highresCntId, i.sortRank, coalesce(dateDiff(day, lastUpdatedDate, @todayDate),0) as freshness, case when i.privateYesNo = 1 then 'Y' else 'N' end privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , STRING_AGG(CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END, ',') AS linked_opr , STRING_AGG(CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END, ',') AS linked_prd , STRING_AGG(CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END, ',') AS linked_sku , STRING_AGG(CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END, ',') AS linked_cdr , STRING_AGG(CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END, ',') AS linked_mfr , cast(concat('IMG',':',i.imgId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , i.internalOnly, i.recordSource , STRING_AGG(CASE WHEN c.child_attrTypeID = 23 THEN c.child_attrID ELSE NULL END, ',') AS permissionGrpIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 7 THEN c.child_attrID ELSE NULL END, ',') AS linked_brokerIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 31 THEN c.child_attrID ELSE NULL END, ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = i.imgId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CASE WHEN c.child_attrTypeID = 34 THEN c.child_attrID ELSE NULL END, ',') AS linked_UserMemberIds from tbl_IMG_Images i with (nolock) left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = i.imgId and li.child_attrTypeID in (41,59,60,63,42) and li.mstr_attrTypeID = 33 left outer JOIN tbl_ORG_AttributeLinks c ON c.mstr_attrID = i.imgId and c.child_attrTypeID in (23,7,31,34) and c.mstr_attrTypeID = 30 left outer join tbl_DOC_Categories dc with (nolock) ON c.child_attrTypeID = 31 AND dc.categoryId = c.child_attrID AND dc.ownerId = @ownerId AND dc.fsl_tablecode = @ownerType left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('IMG',':',i.imgId) where i.ownerId = @ownerId and i.fsl_tablecode = @ownerType GROUP BY i.imgTitle, i.CategoryID, i.contentId, i.imgId, i.fileSize, i.fileExt, i.imgDescription, i.publishDate, i.unpublishDate, i.uploadDate, i.lastUpdatedDate, i.lastUpdatedBy, i.highresCntId, i.sortRank, i.privateYesNo,ast.assetSelectionKey, i.internalOnly, i.recordSource ), filteredRawAssets as ( select ra.* from rawAssets ra where (1=1 or ra.linked_brokerIds in (?) ) ) , assetsWithContentURL1 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL, c.fileName as docFileName, lub.firstname + ' ' + lub.lastname as lastUpdatedByFullName from filteredRawAssets ra LEFT OUTER JOIN tbl_FSPRO_Members lub with (nolock) ON ra.updateUserId = lub.fspro_userId inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) , assetsWithContentURL2 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as thumbnailURL, c.fileName as thnFileName from assetsWithContentURL1 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.thumbnailId and c.contentId != 755 ) , assetsWithContentURL3 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as highresURL, c.fileName as highresCntFileName from assetsWithContentURL2 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.highresCntId and c.contentId != 755 ) , assets as ( select a.*, case when right( a.contentURL, charindex( '.', reverse( a.contentURL )) - 1 ) in ( 'BMP','GIF','JPEG','JPG','PNG','TIF','TIFF','WBMP' ) then left( a.contentURL, len( a.contentURL ) - charindex( '.', reverse( a.contentURL ) )) + '_50' + right( a.contentURL, charindex( '.', reverse( a.contentURL )) ) else null end previewURL from assetsWithContentURL3 a ) , categories as ( select 'OPR' contentDomain, categoryId, categoryDescription from tbl_OPR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'CDR' contentDomain, categoryId, categoryDescription from tbl_CDR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'BPL' contentDomain, categoryId, categoryDescription from tbl_BPL_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'DOC' contentDomain, categoryId, categoryName as categoryDescription from tbl_DOC_Categories with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType ) , assetsWithCategory as ( select a.*, c.categoryDescription from assets a left outer join categories c on c.contentDomain = a.contentDomain and c.categoryId = a.categoryId ) ,industryCategories as ( SELECT categoryId, categoryDescription FROM tbl_PRD_RepositoryCategories with (nolock) WHERE containerType = 'SKU' AND ownerID = 0 AND ownerType = '*' AND left(categoryDescription, 3) <> 'GS1' ) , assetsWithIndustryCategory as ( select a.*, ic.categoryDescription as industryCategoryDescription from assetsWithCategory a left outer join industryCategories ic on ic.categoryId = a.categoryId ) , assetsWithPartner as ( select a.*, opr.companyName as partnerName, a.containerId as partnerId, a.containerType as partnerType, opr.territoryId from assetsWithIndustryCategory a inner join tbl_OPR_CLientOperators opr with (nolock) on opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = a.containerId where a.containerType = 'OPR' union all select a.*, cdr.cdr_dstName as partnerName, a.containerId as partnerId, a.containerType as partnerType, cdr.cdr_territoryId as territoryId from assetsWithIndustryCategory a inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = @ownerId and cdr.fsl_tablecode = @ownerType and cdr.cdr_recordId = a.containerId where a.containerType = 'CDR' union all select a.*, mfr.mfr_name as partnerName, mfr.mfr_id as partnerId, 'MFR' as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a inner join tbl_BRO_Principals bpl with (nolock) on bpl.ownerId = @ownerId and bpl.ownerType = @ownerType and bpl.principalId = a.containerId inner join tblManufacturers mfr with (nolock) on mfr.mfr_id = bpl.mfrId where a.containerType = 'BPL' union all select a.*,cast (null as varchar ) as partnerName, cast( null as integer ) as partnerId, cast( null as varchar ) as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a where coalesce( a.containerType, '???') not in ( 'BPL', 'OPR', 'CDR' ) ), assetsWithUser as ( select a.*, m.email updateUserEmail, m.firstName + ' ' + m.lastName as updateUserFullName, case when len(isnull(a.linked_prd,'')) > 0 or len(isnull(a.linked_sku,'')) > 0 then 'Y' else 'N' end as prdLink, case when len(isnull(a.linked_opr,'')) > 0 then 'Y' else 'N' end as oprLink, case when len(isnull(a.linked_cdr,'')) > 0 then 'Y' else 'N' end as cdrLink, case when len(isnull(a.linked_mfr,'')) > 0 then 'Y' else 'N' end as mfrLink from assetsWithPartner as a left outer join tbl_Fspro_members m with (nolock) on m.ownerId > 0 and m.fspro_userId = a.updateUserId ) , assetsWithUser_mappedData as ( select * from assetsWithUser ) , assetsWithUser_preData as ( select * from assetsWithUser_mappedData with (nolock) where ( ( 1 = 1 ) and ( ( ( [isPublished] = 'Y' ) or ( [isPublished] = 'F' ) ) ) ) ) , assetsWithUser_data as ( select * from assetsWithUser_preData ) , assetsWithUser_return as ( select * from assetsWithUser_data where 1 = 1 order by docTitle offset 0 rows fetch next 50 rows only ) , assetsWithUser_result as ( select ( select count(*) from assetsWithUser_data ) as totalCount, ( select * from assetsWithUser_return for json auto, include_null_values ) as data ) select * from assetsWithUser_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER -- 2026-04-16 12:55:25.981 -- 0 {ts '2026-04-16 12:55:28'} SQL declare @ownerId int = 207 ; declare @ownerType varchar(3) = 'MFR' ; declare @contentPrefix varchar(128) = 'http://dev.fsenablers.com/FSLContent/' ; declare @todayDate datetime = ?; with docCategories as ( SELECT cats.attrID as categoryId, cats.attrDescription AS categoryName FROM tbl_ORG_Attributes cats with (nolock) WHERE cats.OwnerID = @ownerId AND cats.FSL_TableCode = @ownerType AND cats.attrTypeID = 31 ), rawAssets as ( select null contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, cast( null as varchar ) docTitle, cast( null as int) as categoryId, cast( null as int ) contentId, cast( null as int) fileId, 0.00 fileSize, null fileExt, null docDescription, cast( null as datetime ) publishDate, cast( null as datetime ) unpublishDate, 'N' isPublished, cast( null as datetime ) uploadDate, cast( null as datetime ) updateDate, cast( null as int ) updateUserId, 0 restricted, -1 thumbnailId, -1 highresCntId, 99 as sortRank , 0 as freshness , 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr, cast( null as varchar ) as assetSelectionKey, 0 as selectedRecord , 'N' as internalOnly, '' as recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds union all select 'DOC' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, d.docTitle, d.repCategoryId as categoryId, d.contentId, d.docId as fileId, CAST((d.fileSize/1024) AS float) as fileSize , d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, case when d.publishDate is not null and d.publishDate > @todayDate then 'F' when d.publishDate is not null and d.unpublishDate is not null and @todayDate >= d.publishDate and @todayDate <= d.unpublishDate then 'Y' when d.publishDate is not null and d.unpublishDate is null and d.publishDate <= @todayDate then 'Y' when d.publishDate is null then 'N' else 'N' end isPublished, d.uploadDate, d.lastUpdatedDate updateDate, d.lastUpdatedBy updateUserId, 0 restricted, thumbnailId, -1 highresCntId, sortRank , coalesce(dateDiff(day, d.lastUpdatedDate, @todayDate),0) as freshness, case when d.privateYesNo = 1 then 'Y' else 'N' end privateYesNo, ou.firstname + ' ' + ou.lastname as uploadedByFullName, d.keyWords, d.unpublishToDo, d.internalNotes , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END), ',') AS linked_opr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END), ',') AS linked_prd , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END), ',') AS linked_sku , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END), ',') AS linked_cdr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END), ',') AS linked_mfr , cast(concat('DOC',':',d.docId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , d.internalOnly, d.recordSource , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 23 THEN li.child_attrID ELSE NULL END), ',') AS permissionGrpIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 7 THEN li.child_attrID ELSE NULL END), ',') AS linked_brokerIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 31 THEN li.child_attrID ELSE NULL END), ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = d.docId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 34 THEN li.child_attrID ELSE NULL END), ',') AS linked_UserMemberIds from tbl_DOC_Documents d with (nolock) LEFT OUTER JOIN tbl_FSPRO_Members ou with (nolock) ON d.AuthorUserId = ou.fspro_userId left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = d.docId and li.child_attrTypeID in (41,59,60,63,42,23,7,31,34) and li.mstr_attrTypeID = 30 left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('DOC',':',d.docId) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType GROUP BY d.docTitle, d.repCategoryId, d.contentId, d.docId,d.fileSize, d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, d.uploadDate, d.lastUpdatedDate, d.lastUpdatedBy,thumbnailId, sortRank , d.privateYesNo , ou.firstname,ou.lastname, d.keyWords, d.unpublishToDo, d.internalNotes,ast.assetSelectionKey, d.internalOnly, d.recordSource union all select 'IMG' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, i.imgTitle as docTitle, i.categoryId, i.contentId, i.imgId as fileId, CAST((i.fileSize/1024) AS float) as fileSize, i.fileExt, i.imgDescription as docDescription, i.publishDate, i.unpublishDate, case when i.publishDate is not null and i.publishDate > @todayDate then 'F' when i.publishDate is not null and i.unpublishDate is not null and @todayDate >= i.publishDate and @todayDate <= i.unpublishDate then 'Y' when i.publishDate is not null and i.unpublishDate is null and i.publishDate <= @todayDate then 'Y' when i.publishDate is null then 'N' else 'N' end isPublished, i.uploadDate, i.lastUpdatedDate updateDate, i.lastUpdatedBy updateUserId, 0 restricted, -1 thumbnailId, i.highresCntId, i.sortRank, coalesce(dateDiff(day, lastUpdatedDate, @todayDate),0) as freshness, case when i.privateYesNo = 1 then 'Y' else 'N' end privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , STRING_AGG(CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END, ',') AS linked_opr , STRING_AGG(CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END, ',') AS linked_prd , STRING_AGG(CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END, ',') AS linked_sku , STRING_AGG(CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END, ',') AS linked_cdr , STRING_AGG(CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END, ',') AS linked_mfr , cast(concat('IMG',':',i.imgId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , i.internalOnly, i.recordSource , STRING_AGG(CASE WHEN c.child_attrTypeID = 23 THEN c.child_attrID ELSE NULL END, ',') AS permissionGrpIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 7 THEN c.child_attrID ELSE NULL END, ',') AS linked_brokerIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 31 THEN c.child_attrID ELSE NULL END, ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = i.imgId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CASE WHEN c.child_attrTypeID = 34 THEN c.child_attrID ELSE NULL END, ',') AS linked_UserMemberIds from tbl_IMG_Images i with (nolock) left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = i.imgId and li.child_attrTypeID in (41,59,60,63,42) and li.mstr_attrTypeID = 33 left outer JOIN tbl_ORG_AttributeLinks c ON c.mstr_attrID = i.imgId and c.child_attrTypeID in (23,7,31,34) and c.mstr_attrTypeID = 30 left outer join tbl_DOC_Categories dc with (nolock) ON c.child_attrTypeID = 31 AND dc.categoryId = c.child_attrID AND dc.ownerId = @ownerId AND dc.fsl_tablecode = @ownerType left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('IMG',':',i.imgId) where i.ownerId = @ownerId and i.fsl_tablecode = @ownerType GROUP BY i.imgTitle, i.CategoryID, i.contentId, i.imgId, i.fileSize, i.fileExt, i.imgDescription, i.publishDate, i.unpublishDate, i.uploadDate, i.lastUpdatedDate, i.lastUpdatedBy, i.highresCntId, i.sortRank, i.privateYesNo,ast.assetSelectionKey, i.internalOnly, i.recordSource ), filteredRawAssets as ( select ra.* from rawAssets ra where (1=1 or ra.linked_brokerIds in (?) ) ) , assetsWithContentURL1 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL, c.fileName as docFileName, lub.firstname + ' ' + lub.lastname as lastUpdatedByFullName from filteredRawAssets ra LEFT OUTER JOIN tbl_FSPRO_Members lub with (nolock) ON ra.updateUserId = lub.fspro_userId inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) , assetsWithContentURL2 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as thumbnailURL, c.fileName as thnFileName from assetsWithContentURL1 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.thumbnailId and c.contentId != 755 ) , assetsWithContentURL3 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as highresURL, c.fileName as highresCntFileName from assetsWithContentURL2 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.highresCntId and c.contentId != 755 ) , assets as ( select a.*, case when right( a.contentURL, charindex( '.', reverse( a.contentURL )) - 1 ) in ( 'BMP','GIF','JPEG','JPG','PNG','TIF','TIFF','WBMP' ) then left( a.contentURL, len( a.contentURL ) - charindex( '.', reverse( a.contentURL ) )) + '_50' + right( a.contentURL, charindex( '.', reverse( a.contentURL )) ) else null end previewURL from assetsWithContentURL3 a ) , categories as ( select 'OPR' contentDomain, categoryId, categoryDescription from tbl_OPR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'CDR' contentDomain, categoryId, categoryDescription from tbl_CDR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'BPL' contentDomain, categoryId, categoryDescription from tbl_BPL_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'DOC' contentDomain, categoryId, categoryName as categoryDescription from tbl_DOC_Categories with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType ) , assetsWithCategory as ( select a.*, c.categoryDescription from assets a left outer join categories c on c.contentDomain = a.contentDomain and c.categoryId = a.categoryId ) ,industryCategories as ( SELECT categoryId, categoryDescription FROM tbl_PRD_RepositoryCategories with (nolock) WHERE containerType = 'SKU' AND ownerID = 0 AND ownerType = '*' AND left(categoryDescription, 3) <> 'GS1' ) , assetsWithIndustryCategory as ( select a.*, ic.categoryDescription as industryCategoryDescription from assetsWithCategory a left outer join industryCategories ic on ic.categoryId = a.categoryId ) , assetsWithPartner as ( select a.*, opr.companyName as partnerName, a.containerId as partnerId, a.containerType as partnerType, opr.territoryId from assetsWithIndustryCategory a inner join tbl_OPR_CLientOperators opr with (nolock) on opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = a.containerId where a.containerType = 'OPR' union all select a.*, cdr.cdr_dstName as partnerName, a.containerId as partnerId, a.containerType as partnerType, cdr.cdr_territoryId as territoryId from assetsWithIndustryCategory a inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = @ownerId and cdr.fsl_tablecode = @ownerType and cdr.cdr_recordId = a.containerId where a.containerType = 'CDR' union all select a.*, mfr.mfr_name as partnerName, mfr.mfr_id as partnerId, 'MFR' as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a inner join tbl_BRO_Principals bpl with (nolock) on bpl.ownerId = @ownerId and bpl.ownerType = @ownerType and bpl.principalId = a.containerId inner join tblManufacturers mfr with (nolock) on mfr.mfr_id = bpl.mfrId where a.containerType = 'BPL' union all select a.*,cast (null as varchar ) as partnerName, cast( null as integer ) as partnerId, cast( null as varchar ) as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a where coalesce( a.containerType, '???') not in ( 'BPL', 'OPR', 'CDR' ) ), assetsWithUser as ( select a.*, m.email updateUserEmail, m.firstName + ' ' + m.lastName as updateUserFullName, case when len(isnull(a.linked_prd,'')) > 0 or len(isnull(a.linked_sku,'')) > 0 then 'Y' else 'N' end as prdLink, case when len(isnull(a.linked_opr,'')) > 0 then 'Y' else 'N' end as oprLink, case when len(isnull(a.linked_cdr,'')) > 0 then 'Y' else 'N' end as cdrLink, case when len(isnull(a.linked_mfr,'')) > 0 then 'Y' else 'N' end as mfrLink from assetsWithPartner as a left outer join tbl_Fspro_members m with (nolock) on m.ownerId > 0 and m.fspro_userId = a.updateUserId ) , assetsWithUser_mappedData as ( select * from assetsWithUser ) , assetsWithUser_preData as ( select * from assetsWithUser_mappedData with (nolock) where ( ( 1 = 1 ) and ( ( ( [isPublished] = 'Y' ) or ( [isPublished] = 'F' ) ) ) ) ) , assetsWithUser_data as ( select * from assetsWithUser_preData ) , assetsWithUser_return as ( select * from assetsWithUser_data where 1 = 1 order by docTitle offset 0 rows fetch next 10000000 rows only ) , assetsWithUser_result as ( select ( select count(*) from assetsWithUser_data ) as totalCount, ( select * from assetsWithUser_return for json auto, include_null_values ) as data ) select * from assetsWithUser_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER -- 2026-04-16 12:55:28.092 -- 0 {ts '2026-04-16 12:55:30'} SQL declare @ownerId int = 207 ; declare @ownerType varchar(3) = 'MFR' ; declare @contentPrefix varchar(128) = 'http://dev.fsenablers.com/FSLContent/' ; declare @todayDate datetime = ?; with docCategories as ( SELECT cats.attrID as categoryId, cats.attrDescription AS categoryName FROM tbl_ORG_Attributes cats with (nolock) WHERE cats.OwnerID = @ownerId AND cats.FSL_TableCode = @ownerType AND cats.attrTypeID = 31 ), rawAssets as ( select null contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, cast( null as varchar ) docTitle, cast( null as int) as categoryId, cast( null as int ) contentId, cast( null as int) fileId, 0.00 fileSize, null fileExt, null docDescription, cast( null as datetime ) publishDate, cast( null as datetime ) unpublishDate, 'N' isPublished, cast( null as datetime ) uploadDate, cast( null as datetime ) updateDate, cast( null as int ) updateUserId, 0 restricted, -1 thumbnailId, -1 highresCntId, 99 as sortRank , 0 as freshness , 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr, cast( null as varchar ) as assetSelectionKey, 0 as selectedRecord , 'N' as internalOnly, '' as recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds union all select 'DOC' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, d.docTitle, d.repCategoryId as categoryId, d.contentId, d.docId as fileId, CAST((d.fileSize/1024) AS float) as fileSize , d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, case when d.publishDate is not null and d.publishDate > @todayDate then 'F' when d.publishDate is not null and d.unpublishDate is not null and @todayDate >= d.publishDate and @todayDate <= d.unpublishDate then 'Y' when d.publishDate is not null and d.unpublishDate is null and d.publishDate <= @todayDate then 'Y' when d.publishDate is null then 'N' else 'N' end isPublished, d.uploadDate, d.lastUpdatedDate updateDate, d.lastUpdatedBy updateUserId, 0 restricted, thumbnailId, -1 highresCntId, sortRank , coalesce(dateDiff(day, d.lastUpdatedDate, @todayDate),0) as freshness, case when d.privateYesNo = 1 then 'Y' else 'N' end privateYesNo, ou.firstname + ' ' + ou.lastname as uploadedByFullName, d.keyWords, d.unpublishToDo, d.internalNotes , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END), ',') AS linked_opr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END), ',') AS linked_prd , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END), ',') AS linked_sku , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END), ',') AS linked_cdr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END), ',') AS linked_mfr , cast(concat('DOC',':',d.docId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , d.internalOnly, d.recordSource , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 23 THEN li.child_attrID ELSE NULL END), ',') AS permissionGrpIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 7 THEN li.child_attrID ELSE NULL END), ',') AS linked_brokerIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 31 THEN li.child_attrID ELSE NULL END), ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = d.docId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 34 THEN li.child_attrID ELSE NULL END), ',') AS linked_UserMemberIds from tbl_DOC_Documents d with (nolock) LEFT OUTER JOIN tbl_FSPRO_Members ou with (nolock) ON d.AuthorUserId = ou.fspro_userId left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = d.docId and li.child_attrTypeID in (41,59,60,63,42,23,7,31,34) and li.mstr_attrTypeID = 30 left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('DOC',':',d.docId) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType GROUP BY d.docTitle, d.repCategoryId, d.contentId, d.docId,d.fileSize, d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, d.uploadDate, d.lastUpdatedDate, d.lastUpdatedBy,thumbnailId, sortRank , d.privateYesNo , ou.firstname,ou.lastname, d.keyWords, d.unpublishToDo, d.internalNotes,ast.assetSelectionKey, d.internalOnly, d.recordSource union all select 'IMG' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, i.imgTitle as docTitle, i.categoryId, i.contentId, i.imgId as fileId, CAST((i.fileSize/1024) AS float) as fileSize, i.fileExt, i.imgDescription as docDescription, i.publishDate, i.unpublishDate, case when i.publishDate is not null and i.publishDate > @todayDate then 'F' when i.publishDate is not null and i.unpublishDate is not null and @todayDate >= i.publishDate and @todayDate <= i.unpublishDate then 'Y' when i.publishDate is not null and i.unpublishDate is null and i.publishDate <= @todayDate then 'Y' when i.publishDate is null then 'N' else 'N' end isPublished, i.uploadDate, i.lastUpdatedDate updateDate, i.lastUpdatedBy updateUserId, 0 restricted, -1 thumbnailId, i.highresCntId, i.sortRank, coalesce(dateDiff(day, lastUpdatedDate, @todayDate),0) as freshness, case when i.privateYesNo = 1 then 'Y' else 'N' end privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , STRING_AGG(CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END, ',') AS linked_opr , STRING_AGG(CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END, ',') AS linked_prd , STRING_AGG(CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END, ',') AS linked_sku , STRING_AGG(CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END, ',') AS linked_cdr , STRING_AGG(CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END, ',') AS linked_mfr , cast(concat('IMG',':',i.imgId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , i.internalOnly, i.recordSource , STRING_AGG(CASE WHEN c.child_attrTypeID = 23 THEN c.child_attrID ELSE NULL END, ',') AS permissionGrpIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 7 THEN c.child_attrID ELSE NULL END, ',') AS linked_brokerIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 31 THEN c.child_attrID ELSE NULL END, ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = i.imgId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CASE WHEN c.child_attrTypeID = 34 THEN c.child_attrID ELSE NULL END, ',') AS linked_UserMemberIds from tbl_IMG_Images i with (nolock) left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = i.imgId and li.child_attrTypeID in (41,59,60,63,42) and li.mstr_attrTypeID = 33 left outer JOIN tbl_ORG_AttributeLinks c ON c.mstr_attrID = i.imgId and c.child_attrTypeID in (23,7,31,34) and c.mstr_attrTypeID = 30 left outer join tbl_DOC_Categories dc with (nolock) ON c.child_attrTypeID = 31 AND dc.categoryId = c.child_attrID AND dc.ownerId = @ownerId AND dc.fsl_tablecode = @ownerType left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('IMG',':',i.imgId) where i.ownerId = @ownerId and i.fsl_tablecode = @ownerType GROUP BY i.imgTitle, i.CategoryID, i.contentId, i.imgId, i.fileSize, i.fileExt, i.imgDescription, i.publishDate, i.unpublishDate, i.uploadDate, i.lastUpdatedDate, i.lastUpdatedBy, i.highresCntId, i.sortRank, i.privateYesNo,ast.assetSelectionKey, i.internalOnly, i.recordSource ), filteredRawAssets as ( select ra.* from rawAssets ra where (1=1 or ra.linked_brokerIds in (?) ) ) , assetsWithContentURL1 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL, c.fileName as docFileName, lub.firstname + ' ' + lub.lastname as lastUpdatedByFullName from filteredRawAssets ra LEFT OUTER JOIN tbl_FSPRO_Members lub with (nolock) ON ra.updateUserId = lub.fspro_userId inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) , assetsWithContentURL2 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as thumbnailURL, c.fileName as thnFileName from assetsWithContentURL1 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.thumbnailId and c.contentId != 755 ) , assetsWithContentURL3 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as highresURL, c.fileName as highresCntFileName from assetsWithContentURL2 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.highresCntId and c.contentId != 755 ) , assets as ( select a.*, case when right( a.contentURL, charindex( '.', reverse( a.contentURL )) - 1 ) in ( 'BMP','GIF','JPEG','JPG','PNG','TIF','TIFF','WBMP' ) then left( a.contentURL, len( a.contentURL ) - charindex( '.', reverse( a.contentURL ) )) + '_50' + right( a.contentURL, charindex( '.', reverse( a.contentURL )) ) else null end previewURL from assetsWithContentURL3 a ) , categories as ( select 'OPR' contentDomain, categoryId, categoryDescription from tbl_OPR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'CDR' contentDomain, categoryId, categoryDescription from tbl_CDR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'BPL' contentDomain, categoryId, categoryDescription from tbl_BPL_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'DOC' contentDomain, categoryId, categoryName as categoryDescription from tbl_DOC_Categories with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType ) , assetsWithCategory as ( select a.*, c.categoryDescription from assets a left outer join categories c on c.contentDomain = a.contentDomain and c.categoryId = a.categoryId ) ,industryCategories as ( SELECT categoryId, categoryDescription FROM tbl_PRD_RepositoryCategories with (nolock) WHERE containerType = 'SKU' AND ownerID = 0 AND ownerType = '*' AND left(categoryDescription, 3) <> 'GS1' ) , assetsWithIndustryCategory as ( select a.*, ic.categoryDescription as industryCategoryDescription from assetsWithCategory a left outer join industryCategories ic on ic.categoryId = a.categoryId ) , assetsWithPartner as ( select a.*, opr.companyName as partnerName, a.containerId as partnerId, a.containerType as partnerType, opr.territoryId from assetsWithIndustryCategory a inner join tbl_OPR_CLientOperators opr with (nolock) on opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = a.containerId where a.containerType = 'OPR' union all select a.*, cdr.cdr_dstName as partnerName, a.containerId as partnerId, a.containerType as partnerType, cdr.cdr_territoryId as territoryId from assetsWithIndustryCategory a inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = @ownerId and cdr.fsl_tablecode = @ownerType and cdr.cdr_recordId = a.containerId where a.containerType = 'CDR' union all select a.*, mfr.mfr_name as partnerName, mfr.mfr_id as partnerId, 'MFR' as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a inner join tbl_BRO_Principals bpl with (nolock) on bpl.ownerId = @ownerId and bpl.ownerType = @ownerType and bpl.principalId = a.containerId inner join tblManufacturers mfr with (nolock) on mfr.mfr_id = bpl.mfrId where a.containerType = 'BPL' union all select a.*,cast (null as varchar ) as partnerName, cast( null as integer ) as partnerId, cast( null as varchar ) as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a where coalesce( a.containerType, '???') not in ( 'BPL', 'OPR', 'CDR' ) ), assetsWithUser as ( select a.*, m.email updateUserEmail, m.firstName + ' ' + m.lastName as updateUserFullName, case when len(isnull(a.linked_prd,'')) > 0 or len(isnull(a.linked_sku,'')) > 0 then 'Y' else 'N' end as prdLink, case when len(isnull(a.linked_opr,'')) > 0 then 'Y' else 'N' end as oprLink, case when len(isnull(a.linked_cdr,'')) > 0 then 'Y' else 'N' end as cdrLink, case when len(isnull(a.linked_mfr,'')) > 0 then 'Y' else 'N' end as mfrLink from assetsWithPartner as a left outer join tbl_Fspro_members m with (nolock) on m.ownerId > 0 and m.fspro_userId = a.updateUserId ) , assetsWithUser_mappedData as ( select * from assetsWithUser ) , assetsWithUser_preData as ( select * from assetsWithUser_mappedData with (nolock) where ( ( [industryCategoryDescription] = 'Menu / Recipe Ideas' ) and ( ( 1 = 1 ) and ( ( ( [isPublished] = 'Y' ) or ( [isPublished] = 'F' ) ) ) ) ) ) , assetsWithUser_data as ( select * from assetsWithUser_preData ) , assetsWithUser_return as ( select * from assetsWithUser_data where 1 = 1 order by docTitle offset 0 rows fetch next 50 rows only ) , assetsWithUser_result as ( select ( select count(*) from assetsWithUser_data ) as totalCount, ( select * from assetsWithUser_return for json auto, include_null_values ) as data ) select * from assetsWithUser_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER -- 2026-04-16 12:55:30.111 -- 0 {ts '2026-04-16 12:55:36'} SQL declare @ownerId int = 207 ; declare @ownerType varchar(3) = 'MFR' ; declare @contentPrefix varchar(128) = 'http://dev.fsenablers.com/FSLContent/' ; declare @todayDate datetime = ?; with docCategories as ( SELECT cats.attrID as categoryId, cats.attrDescription AS categoryName FROM tbl_ORG_Attributes cats with (nolock) WHERE cats.OwnerID = @ownerId AND cats.FSL_TableCode = @ownerType AND cats.attrTypeID = 31 ), rawAssets as ( select null contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, cast( null as varchar ) docTitle, cast( null as int) as categoryId, cast( null as int ) contentId, cast( null as int) fileId, 0.00 fileSize, null fileExt, null docDescription, cast( null as datetime ) publishDate, cast( null as datetime ) unpublishDate, 'N' isPublished, cast( null as datetime ) uploadDate, cast( null as datetime ) updateDate, cast( null as int ) updateUserId, 0 restricted, -1 thumbnailId, -1 highresCntId, 99 as sortRank , 0 as freshness , 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr, cast( null as varchar ) as assetSelectionKey, 0 as selectedRecord , 'N' as internalOnly, '' as recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds union all select 'DOC' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, d.docTitle, d.repCategoryId as categoryId, d.contentId, d.docId as fileId, CAST((d.fileSize/1024) AS float) as fileSize , d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, case when d.publishDate is not null and d.publishDate > @todayDate then 'F' when d.publishDate is not null and d.unpublishDate is not null and @todayDate >= d.publishDate and @todayDate <= d.unpublishDate then 'Y' when d.publishDate is not null and d.unpublishDate is null and d.publishDate <= @todayDate then 'Y' when d.publishDate is null then 'N' else 'N' end isPublished, d.uploadDate, d.lastUpdatedDate updateDate, d.lastUpdatedBy updateUserId, 0 restricted, thumbnailId, -1 highresCntId, sortRank , coalesce(dateDiff(day, d.lastUpdatedDate, @todayDate),0) as freshness, case when d.privateYesNo = 1 then 'Y' else 'N' end privateYesNo, ou.firstname + ' ' + ou.lastname as uploadedByFullName, d.keyWords, d.unpublishToDo, d.internalNotes , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END), ',') AS linked_opr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END), ',') AS linked_prd , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END), ',') AS linked_sku , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END), ',') AS linked_cdr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END), ',') AS linked_mfr , cast(concat('DOC',':',d.docId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , d.internalOnly, d.recordSource , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 23 THEN li.child_attrID ELSE NULL END), ',') AS permissionGrpIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 7 THEN li.child_attrID ELSE NULL END), ',') AS linked_brokerIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 31 THEN li.child_attrID ELSE NULL END), ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = d.docId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 34 THEN li.child_attrID ELSE NULL END), ',') AS linked_UserMemberIds from tbl_DOC_Documents d with (nolock) LEFT OUTER JOIN tbl_FSPRO_Members ou with (nolock) ON d.AuthorUserId = ou.fspro_userId left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = d.docId and li.child_attrTypeID in (41,59,60,63,42,23,7,31,34) and li.mstr_attrTypeID = 30 left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('DOC',':',d.docId) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType GROUP BY d.docTitle, d.repCategoryId, d.contentId, d.docId,d.fileSize, d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, d.uploadDate, d.lastUpdatedDate, d.lastUpdatedBy,thumbnailId, sortRank , d.privateYesNo , ou.firstname,ou.lastname, d.keyWords, d.unpublishToDo, d.internalNotes,ast.assetSelectionKey, d.internalOnly, d.recordSource union all select 'IMG' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, i.imgTitle as docTitle, i.categoryId, i.contentId, i.imgId as fileId, CAST((i.fileSize/1024) AS float) as fileSize, i.fileExt, i.imgDescription as docDescription, i.publishDate, i.unpublishDate, case when i.publishDate is not null and i.publishDate > @todayDate then 'F' when i.publishDate is not null and i.unpublishDate is not null and @todayDate >= i.publishDate and @todayDate <= i.unpublishDate then 'Y' when i.publishDate is not null and i.unpublishDate is null and i.publishDate <= @todayDate then 'Y' when i.publishDate is null then 'N' else 'N' end isPublished, i.uploadDate, i.lastUpdatedDate updateDate, i.lastUpdatedBy updateUserId, 0 restricted, -1 thumbnailId, i.highresCntId, i.sortRank, coalesce(dateDiff(day, lastUpdatedDate, @todayDate),0) as freshness, case when i.privateYesNo = 1 then 'Y' else 'N' end privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , STRING_AGG(CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END, ',') AS linked_opr , STRING_AGG(CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END, ',') AS linked_prd , STRING_AGG(CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END, ',') AS linked_sku , STRING_AGG(CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END, ',') AS linked_cdr , STRING_AGG(CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END, ',') AS linked_mfr , cast(concat('IMG',':',i.imgId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , i.internalOnly, i.recordSource , STRING_AGG(CASE WHEN c.child_attrTypeID = 23 THEN c.child_attrID ELSE NULL END, ',') AS permissionGrpIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 7 THEN c.child_attrID ELSE NULL END, ',') AS linked_brokerIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 31 THEN c.child_attrID ELSE NULL END, ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = i.imgId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CASE WHEN c.child_attrTypeID = 34 THEN c.child_attrID ELSE NULL END, ',') AS linked_UserMemberIds from tbl_IMG_Images i with (nolock) left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = i.imgId and li.child_attrTypeID in (41,59,60,63,42) and li.mstr_attrTypeID = 33 left outer JOIN tbl_ORG_AttributeLinks c ON c.mstr_attrID = i.imgId and c.child_attrTypeID in (23,7,31,34) and c.mstr_attrTypeID = 30 left outer join tbl_DOC_Categories dc with (nolock) ON c.child_attrTypeID = 31 AND dc.categoryId = c.child_attrID AND dc.ownerId = @ownerId AND dc.fsl_tablecode = @ownerType left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('IMG',':',i.imgId) where i.ownerId = @ownerId and i.fsl_tablecode = @ownerType GROUP BY i.imgTitle, i.CategoryID, i.contentId, i.imgId, i.fileSize, i.fileExt, i.imgDescription, i.publishDate, i.unpublishDate, i.uploadDate, i.lastUpdatedDate, i.lastUpdatedBy, i.highresCntId, i.sortRank, i.privateYesNo,ast.assetSelectionKey, i.internalOnly, i.recordSource union all select 'ASC' contentDomain, containerId, containerType, docTitle, category as categoryId, o.contentId, o.fileId, CAST((fileSize/1024) AS float) as fileSize, fileExt, docDescription, publishDate, unpublishDate, case when o.publishDate is not null and o.unpublishDate is not null and @todayDate between o.publishDate and o.unpublishDate then 'Y' when o.publishDate is not null and o.unpublishDate is null and o.publishDate <= @todayDate then 'Y' when o.publishDate is not null and o.publishDate > @todayDate then 'F' when o.publishDate is null then 'N' else 'N' end isPublished, uploadDate, updateDate, updateUserId, case when restricted = 'Y' then 1 else 0 end restricted, -1 thumbnailId, -1 highresCntId, sortRank, coalesce( dateDiff(day, updateDate, @todayDate),0) as freshness, 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr , cast(concat('ASC',':',o.fileId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , o.internalOnly, o.recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds from tbl_ASC_Repository o with (nolock) left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('ASC',':',o.fileId) where o.ownerId = @ownerId and o.ownerType = @ownerType union all select 'BCRM' contentDomain, containerId, containerType, docTitle, category as categoryId, o.contentId, o.fileId, CAST((fileSize/1024) AS float) as fileSize, fileExt, docDescription, publishDate, unpublishDate, case when o.publishDate is not null and o.unpublishDate is not null and @todayDate between o.publishDate and o.unpublishDate then 'Y' when o.publishDate is not null and o.unpublishDate is null and o.publishDate <= @todayDate then 'Y' when o.publishDate is not null and o.publishDate > @todayDate then 'F' when o.publishDate is null then 'N' else 'N' end isPublished, uploadDate, updateDate, updateUserId, case when restricted = 'Y' then 1 else 0 end restricted, -1 thumbnailId, -1 highresCntId, sortRank, coalesce( dateDiff(day, updateDate, @todayDate),0) as freshness, 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr , cast(concat('BCRM',':',o.fileId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , o.internalOnly, o.recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds from tbl_BCRM_Repository o with (nolock) left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('BCRM',':',o.fileId) where o.ownerId = @ownerId and o.ownerType = @ownerType union all select 'BID' contentDomain, containerId, containerType, docTitle, category as categoryId, o.contentId, o.fileId, CAST((fileSize/1024) AS float) as fileSize, fileExt, docDescription, publishDate, unpublishDate, case when o.publishDate is not null and o.unpublishDate is not null and @todayDate between o.publishDate and o.unpublishDate then 'Y' when o.publishDate is not null and o.unpublishDate is null and o.publishDate <= @todayDate then 'Y' when o.publishDate is not null and o.publishDate > @todayDate then 'F' when o.publishDate is null then 'N' else 'N' end isPublished, uploadDate, updateDate, updateUserId, case when restricted = 'Y' then 1 else 0 end restricted, -1 thumbnailId, -1 highresCntId, sortRank, coalesce( dateDiff(day, updateDate, @todayDate),0) as freshness, 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr , cast(concat('BID',':',o.fileId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , o.internalOnly, o.recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds from tbl_BID_Repository o with (nolock) left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('BID',':',o.fileId) where o.ownerId = @ownerId and o.ownerType = @ownerType union all select 'BPL' contentDomain, containerId, containerType, docTitle, category as categoryId, o.contentId, o.fileId, CAST((fileSize/1024) AS float) as fileSize, fileExt, docDescription, publishDate, unpublishDate, case when o.publishDate is not null and o.unpublishDate is not null and @todayDate between o.publishDate and o.unpublishDate then 'Y' when o.publishDate is not null and o.unpublishDate is null and o.publishDate <= @todayDate then 'Y' when o.publishDate is not null and o.publishDate > @todayDate then 'F' when o.publishDate is null then 'N' else 'N' end isPublished, uploadDate, updateDate, updateUserId, case when restricted = 'Y' then 1 else 0 end restricted, -1 thumbnailId, -1 highresCntId, sortRank, coalesce( dateDiff(day, updateDate, @todayDate),0) as freshness, 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr , cast(concat('BPL',':',o.fileId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , o.internalOnly, o.recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds from tbl_BPL_Repository o with (nolock) left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('BPL',':',o.fileId) where o.ownerId = @ownerId and o.ownerType = @ownerType union all select 'BRO' contentDomain, containerId, containerType, docTitle, category as categoryId, o.contentId, o.fileId, CAST((fileSize/1024) AS float) as fileSize, fileExt, docDescription, publishDate, unpublishDate, case when o.publishDate is not null and o.unpublishDate is not null and @todayDate between o.publishDate and o.unpublishDate then 'Y' when o.publishDate is not null and o.unpublishDate is null and o.publishDate <= @todayDate then 'Y' when o.publishDate is not null and o.publishDate > @todayDate then 'F' when o.publishDate is null then 'N' else 'N' end isPublished, uploadDate, updateDate, updateUserId, case when restricted = 'Y' then 1 else 0 end restricted, -1 thumbnailId, -1 highresCntId, sortRank, coalesce( dateDiff(day, updateDate, @todayDate),0) as freshness, 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr , cast(concat('BRO',':',o.fileId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , o.internalOnly, o.recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds from tbl_BRO_Repository o with (nolock) left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('BRO',':',o.fileId) where o.ownerId = @ownerId and o.ownerType = @ownerType union all select 'BYG' contentDomain, containerId, containerType, docTitle, category as categoryId, o.contentId, o.fileId, CAST((fileSize/1024) AS float) as fileSize, fileExt, docDescription, publishDate, unpublishDate, case when o.publishDate is not null and o.unpublishDate is not null and @todayDate between o.publishDate and o.unpublishDate then 'Y' when o.publishDate is not null and o.unpublishDate is null and o.publishDate <= @todayDate then 'Y' when o.publishDate is not null and o.publishDate > @todayDate then 'F' when o.publishDate is null then 'N' else 'N' end isPublished, uploadDate, updateDate, updateUserId, case when restricted = 'Y' then 1 else 0 end restricted, -1 thumbnailId, -1 highresCntId, sortRank, coalesce( dateDiff(day, updateDate, @todayDate),0) as freshness, 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr , cast(concat('BYG',':',o.fileId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , o.internalOnly, o.recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds from tbl_BYG_Repository o with (nolock) left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('BYG',':',o.fileId) where o.ownerId = @ownerId and o.ownerType = @ownerType union all select 'CDR' contentDomain, containerId, containerType, docTitle, category as categoryId, o.contentId, o.fileId, CAST((fileSize/1024) AS float) as fileSize, fileExt, docDescription, publishDate, unpublishDate, case when o.publishDate is not null and o.unpublishDate is not null and @todayDate between o.publishDate and o.unpublishDate then 'Y' when o.publishDate is not null and o.unpublishDate is null and o.publishDate <= @todayDate then 'Y' when o.publishDate is not null and o.publishDate > @todayDate then 'F' when o.publishDate is null then 'N' else 'N' end isPublished, uploadDate, updateDate, updateUserId, case when restricted = 'Y' then 1 else 0 end restricted, -1 thumbnailId, -1 highresCntId, sortRank, coalesce( dateDiff(day, updateDate, @todayDate),0) as freshness, 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr , cast(concat('CDR',':',o.fileId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , o.internalOnly, o.recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds from tbl_CDR_Repository o with (nolock) left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('CDR',':',o.fileId) where o.ownerId = @ownerId and o.ownerType = @ownerType union all select 'CHO' contentDomain, containerId, containerType, docTitle, category as categoryId, o.contentId, o.fileId, CAST((fileSize/1024) AS float) as fileSize, fileExt, docDescription, publishDate, unpublishDate, case when o.publishDate is not null and o.unpublishDate is not null and @todayDate between o.publishDate and o.unpublishDate then 'Y' when o.publishDate is not null and o.unpublishDate is null and o.publishDate <= @todayDate then 'Y' when o.publishDate is not null and o.publishDate > @todayDate then 'F' when o.publishDate is null then 'N' else 'N' end isPublished, uploadDate, updateDate, updateUserId, case when restricted = 'Y' then 1 else 0 end restricted, -1 thumbnailId, -1 highresCntId, sortRank, coalesce( dateDiff(day, updateDate, @todayDate),0) as freshness, 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr , cast(concat('CHO',':',o.fileId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , o.internalOnly, o.recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds from tbl_CHO_Repository o with (nolock) left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('CHO',':',o.fileId) where o.ownerId = @ownerId and o.ownerType = @ownerType union all select 'CMP' contentDomain, containerId, containerType, docTitle, category as categoryId, o.contentId, o.fileId, CAST((fileSize/1024) AS float) as fileSize, fileExt, docDescription, publishDate, unpublishDate, case when o.publishDate is not null and o.unpublishDate is not null and @todayDate between o.publishDate and o.unpublishDate then 'Y' when o.publishDate is not null and o.unpublishDate is null and o.publishDate <= @todayDate then 'Y' when o.publishDate is not null and o.publishDate > @todayDate then 'F' when o.publishDate is null then 'N' else 'N' end isPublished, uploadDate, updateDate, updateUserId, case when restricted = 'Y' then 1 else 0 end restricted, -1 thumbnailId, -1 highresCntId, sortRank, coalesce( dateDiff(day, updateDate, @todayDate),0) as freshness, 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr , cast(concat('CMP',':',o.fileId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , o.internalOnly, o.recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds from tbl_CMP_Repository o with (nolock) left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('CMP',':',o.fileId) where o.ownerId = @ownerId and o.ownerType = @ownerType union all select 'CRM' contentDomain, containerId, containerType, docTitle, category as categoryId, o.contentId, o.fileId, CAST((fileSize/1024) AS float) as fileSize, fileExt, docDescription, publishDate, unpublishDate, case when o.publishDate is not null and o.unpublishDate is not null and @todayDate between o.publishDate and o.unpublishDate then 'Y' when o.publishDate is not null and o.unpublishDate is null and o.publishDate <= @todayDate then 'Y' when o.publishDate is not null and o.publishDate > @todayDate then 'F' when o.publishDate is null then 'N' else 'N' end isPublished, uploadDate, updateDate, updateUserId, case when restricted = 'Y' then 1 else 0 end restricted, -1 thumbnailId, -1 highresCntId, sortRank, coalesce( dateDiff(day, updateDate, @todayDate),0) as freshness, 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr , cast(concat('CRM',':',o.fileId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , o.internalOnly, o.recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds from tbl_CRM_Repository o with (nolock) left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('CRM',':',o.fileId) where o.ownerId = @ownerId and o.ownerType = @ownerType union all select 'CRO' contentDomain, containerId, containerType, docTitle, category as categoryId, o.contentId, o.fileId, CAST((fileSize/1024) AS float) as fileSize, fileExt, docDescription, publishDate, unpublishDate, case when o.publishDate is not null and o.unpublishDate is not null and @todayDate between o.publishDate and o.unpublishDate then 'Y' when o.publishDate is not null and o.unpublishDate is null and o.publishDate <= @todayDate then 'Y' when o.publishDate is not null and o.publishDate > @todayDate then 'F' when o.publishDate is null then 'N' else 'N' end isPublished, uploadDate, updateDate, updateUserId, case when restricted = 'Y' then 1 else 0 end restricted, -1 thumbnailId, -1 highresCntId, sortRank, coalesce( dateDiff(day, updateDate, @todayDate),0) as freshness, 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr , cast(concat('CRO',':',o.fileId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , o.internalOnly, o.recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds from tbl_CRO_Repository o with (nolock) left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('CRO',':',o.fileId) where o.ownerId = @ownerId and o.ownerType = @ownerType union all select 'DSK' contentDomain, containerId, containerType, docTitle, category as categoryId, o.contentId, o.fileId, CAST((fileSize/1024) AS float) as fileSize, fileExt, docDescription, publishDate, unpublishDate, case when o.publishDate is not null and o.unpublishDate is not null and @todayDate between o.publishDate and o.unpublishDate then 'Y' when o.publishDate is not null and o.unpublishDate is null and o.publishDate <= @todayDate then 'Y' when o.publishDate is not null and o.publishDate > @todayDate then 'F' when o.publishDate is null then 'N' else 'N' end isPublished, uploadDate, updateDate, updateUserId, case when restricted = 'Y' then 1 else 0 end restricted, -1 thumbnailId, -1 highresCntId, sortRank, coalesce( dateDiff(day, updateDate, @todayDate),0) as freshness, 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr , cast(concat('DSK',':',o.fileId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , o.internalOnly, o.recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds from tbl_DSK_Repository o with (nolock) left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('DSK',':',o.fileId) where o.ownerId = @ownerId and o.ownerType = @ownerType union all select 'DST' contentDomain, containerId, containerType, docTitle, category as categoryId, o.contentId, o.fileId, CAST((fileSize/1024) AS float) as fileSize, fileExt, docDescription, publishDate, unpublishDate, case when o.publishDate is not null and o.unpublishDate is not null and @todayDate between o.publishDate and o.unpublishDate then 'Y' when o.publishDate is not null and o.unpublishDate is null and o.publishDate <= @todayDate then 'Y' when o.publishDate is not null and o.publishDate > @todayDate then 'F' when o.publishDate is null then 'N' else 'N' end isPublished, uploadDate, updateDate, updateUserId, case when restricted = 'Y' then 1 else 0 end restricted, -1 thumbnailId, -1 highresCntId, sortRank, coalesce( dateDiff(day, updateDate, @todayDate),0) as freshness, 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr , cast(concat('DST',':',o.fileId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , o.internalOnly, o.recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds from tbl_DST_Repository o with (nolock) left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('DST',':',o.fileId) where o.ownerId = @ownerId and o.ownerType = @ownerType union all select 'EML' contentDomain, containerId, containerType, docTitle, category as categoryId, o.contentId, o.fileId, CAST((fileSize/1024) AS float) as fileSize, fileExt, docDescription, publishDate, unpublishDate, case when o.publishDate is not null and o.unpublishDate is not null and @todayDate between o.publishDate and o.unpublishDate then 'Y' when o.publishDate is not null and o.unpublishDate is null and o.publishDate <= @todayDate then 'Y' when o.publishDate is not null and o.publishDate > @todayDate then 'F' when o.publishDate is null then 'N' else 'N' end isPublished, uploadDate, updateDate, updateUserId, case when restricted = 'Y' then 1 else 0 end restricted, -1 thumbnailId, -1 highresCntId, sortRank, coalesce( dateDiff(day, updateDate, @todayDate),0) as freshness, 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr , cast(concat('EML',':',o.fileId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , o.internalOnly, o.recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds from tbl_EML_Repository o with (nolock) left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('EML',':',o.fileId) where o.ownerId = @ownerId and o.ownerType = @ownerType union all select 'EVT' contentDomain, containerId, containerType, docTitle, category as categoryId, o.contentId, o.fileId, CAST((fileSize/1024) AS float) as fileSize, fileExt, docDescription, publishDate, unpublishDate, case when o.publishDate is not null and o.unpublishDate is not null and @todayDate between o.publishDate and o.unpublishDate then 'Y' when o.publishDate is not null and o.unpublishDate is null and o.publishDate <= @todayDate then 'Y' when o.publishDate is not null and o.publishDate > @todayDate then 'F' when o.publishDate is null then 'N' else 'N' end isPublished, uploadDate, updateDate, updateUserId, case when restricted = 'Y' then 1 else 0 end restricted, -1 thumbnailId, -1 highresCntId, sortRank, coalesce( dateDiff(day, updateDate, @todayDate),0) as freshness, 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr , cast(concat('EVT',':',o.fileId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , o.internalOnly, o.recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds from tbl_EVT_Repository o with (nolock) left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('EVT',':',o.fileId) where o.ownerId = @ownerId and o.ownerType = @ownerType union all select 'FSPRO' contentDomain, containerId, containerType, docTitle, category as categoryId, o.contentId, o.fileId, CAST((fileSize/1024) AS float) as fileSize, fileExt, docDescription, publishDate, unpublishDate, case when o.publishDate is not null and o.unpublishDate is not null and @todayDate between o.publishDate and o.unpublishDate then 'Y' when o.publishDate is not null and o.unpublishDate is null and o.publishDate <= @todayDate then 'Y' when o.publishDate is not null and o.publishDate > @todayDate then 'F' when o.publishDate is null then 'N' else 'N' end isPublished, uploadDate, updateDate, updateUserId, case when restricted = 'Y' then 1 else 0 end restricted, -1 thumbnailId, -1 highresCntId, sortRank, coalesce( dateDiff(day, updateDate, @todayDate),0) as freshness, 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr , cast(concat('FSPRO',':',o.fileId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , o.internalOnly, o.recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds from tbl_FSPro_Repository o with (nolock) left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('FSPRO',':',o.fileId) where o.ownerId = @ownerId and o.ownerType = @ownerType union all select 'GTM' contentDomain, containerId, containerType, docTitle, category as categoryId, o.contentId, o.fileId, CAST((fileSize/1024) AS float) as fileSize, fileExt, docDescription, publishDate, unpublishDate, case when o.publishDate is not null and o.unpublishDate is not null and @todayDate between o.publishDate and o.unpublishDate then 'Y' when o.publishDate is not null and o.unpublishDate is null and o.publishDate <= @todayDate then 'Y' when o.publishDate is not null and o.publishDate > @todayDate then 'F' when o.publishDate is null then 'N' else 'N' end isPublished, uploadDate, updateDate, updateUserId, case when restricted = 'Y' then 1 else 0 end restricted, -1 thumbnailId, -1 highresCntId, sortRank, coalesce( dateDiff(day, updateDate, @todayDate),0) as freshness, 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr , cast(concat('GTM',':',o.fileId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , o.internalOnly, o.recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds from tbl_GTM_Repository o with (nolock) left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('GTM',':',o.fileId) where o.ownerId = @ownerId and o.ownerType = @ownerType union all select 'HLP' contentDomain, containerId, containerType, docTitle, category as categoryId, o.contentId, o.fileId, CAST((fileSize/1024) AS float) as fileSize, fileExt, docDescription, publishDate, unpublishDate, case when o.publishDate is not null and o.unpublishDate is not null and @todayDate between o.publishDate and o.unpublishDate then 'Y' when o.publishDate is not null and o.unpublishDate is null and o.publishDate <= @todayDate then 'Y' when o.publishDate is not null and o.publishDate > @todayDate then 'F' when o.publishDate is null then 'N' else 'N' end isPublished, uploadDate, updateDate, updateUserId, case when restricted = 'Y' then 1 else 0 end restricted, -1 thumbnailId, -1 highresCntId, sortRank, coalesce( dateDiff(day, updateDate, @todayDate),0) as freshness, 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr , cast(concat('HLP',':',o.fileId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , o.internalOnly, o.recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds from tbl_HLP_Repository o with (nolock) left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('HLP',':',o.fileId) where o.ownerId = @ownerId and o.ownerType = @ownerType union all select 'LFS' contentDomain, containerId, containerType, docTitle, category as categoryId, o.contentId, o.fileId, CAST((fileSize/1024) AS float) as fileSize, fileExt, docDescription, publishDate, unpublishDate, case when o.publishDate is not null and o.unpublishDate is not null and @todayDate between o.publishDate and o.unpublishDate then 'Y' when o.publishDate is not null and o.unpublishDate is null and o.publishDate <= @todayDate then 'Y' when o.publishDate is not null and o.publishDate > @todayDate then 'F' when o.publishDate is null then 'N' else 'N' end isPublished, uploadDate, updateDate, updateUserId, case when restricted = 'Y' then 1 else 0 end restricted, -1 thumbnailId, -1 highresCntId, sortRank, coalesce( dateDiff(day, updateDate, @todayDate),0) as freshness, 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr , cast(concat('LFS',':',o.fileId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , o.internalOnly, o.recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds from tbl_LFS_Repository o with (nolock) left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('LFS',':',o.fileId) where o.ownerId = @ownerId and o.ownerType = @ownerType union all select 'MFR' contentDomain, containerId, containerType, docTitle, category as categoryId, o.contentId, o.fileId, CAST((fileSize/1024) AS float) as fileSize, fileExt, docDescription, publishDate, unpublishDate, case when o.publishDate is not null and o.unpublishDate is not null and @todayDate between o.publishDate and o.unpublishDate then 'Y' when o.publishDate is not null and o.unpublishDate is null and o.publishDate <= @todayDate then 'Y' when o.publishDate is not null and o.publishDate > @todayDate then 'F' when o.publishDate is null then 'N' else 'N' end isPublished, uploadDate, updateDate, updateUserId, case when restricted = 'Y' then 1 else 0 end restricted, -1 thumbnailId, -1 highresCntId, sortRank, coalesce( dateDiff(day, updateDate, @todayDate),0) as freshness, 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr , cast(concat('MFR',':',o.fileId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , o.internalOnly, o.recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds from tbl_MFR_Repository o with (nolock) left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('MFR',':',o.fileId) where o.ownerId = @ownerId and o.ownerType = @ownerType union all select 'MGM' contentDomain, containerId, containerType, docTitle, category as categoryId, o.contentId, o.fileId, CAST((fileSize/1024) AS float) as fileSize, fileExt, docDescription, publishDate, unpublishDate, case when o.publishDate is not null and o.unpublishDate is not null and @todayDate between o.publishDate and o.unpublishDate then 'Y' when o.publishDate is not null and o.unpublishDate is null and o.publishDate <= @todayDate then 'Y' when o.publishDate is not null and o.publishDate > @todayDate then 'F' when o.publishDate is null then 'N' else 'N' end isPublished, uploadDate, updateDate, updateUserId, case when restricted = 'Y' then 1 else 0 end restricted, -1 thumbnailId, -1 highresCntId, sortRank, coalesce( dateDiff(day, updateDate, @todayDate),0) as freshness, 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr , cast(concat('MGM',':',o.fileId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , o.internalOnly, o.recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds from tbl_MGM_Repository o with (nolock) left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('MGM',':',o.fileId) where o.ownerId = @ownerId and o.ownerType = @ownerType union all select 'OPR' contentDomain, containerId, containerType, docTitle, category as categoryId, o.contentId, o.fileId, CAST((fileSize/1024) AS float) as fileSize, fileExt, docDescription, publishDate, unpublishDate, case when o.publishDate is not null and o.unpublishDate is not null and @todayDate between o.publishDate and o.unpublishDate then 'Y' when o.publishDate is not null and o.unpublishDate is null and o.publishDate <= @todayDate then 'Y' when o.publishDate is not null and o.publishDate > @todayDate then 'F' when o.publishDate is null then 'N' else 'N' end isPublished, uploadDate, updateDate, updateUserId, case when restricted = 'Y' then 1 else 0 end restricted, -1 thumbnailId, -1 highresCntId, sortRank, coalesce( dateDiff(day, updateDate, @todayDate),0) as freshness, 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr , cast(concat('OPR',':',o.fileId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , o.internalOnly, o.recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds from tbl_OPR_Repository o with (nolock) left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('OPR',':',o.fileId) where o.ownerId = @ownerId and o.ownerType = @ownerType union all select 'PCS' contentDomain, containerId, containerType, docTitle, category as categoryId, o.contentId, o.fileId, CAST((fileSize/1024) AS float) as fileSize, fileExt, docDescription, publishDate, unpublishDate, case when o.publishDate is not null and o.unpublishDate is not null and @todayDate between o.publishDate and o.unpublishDate then 'Y' when o.publishDate is not null and o.unpublishDate is null and o.publishDate <= @todayDate then 'Y' when o.publishDate is not null and o.publishDate > @todayDate then 'F' when o.publishDate is null then 'N' else 'N' end isPublished, uploadDate, updateDate, updateUserId, case when restricted = 'Y' then 1 else 0 end restricted, -1 thumbnailId, -1 highresCntId, sortRank, coalesce( dateDiff(day, updateDate, @todayDate),0) as freshness, 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr , cast(concat('PCS',':',o.fileId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , o.internalOnly, o.recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds from tbl_PCS_Repository o with (nolock) left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('PCS',':',o.fileId) where o.ownerId = @ownerId and o.ownerType = @ownerType union all select 'POS' contentDomain, containerId, containerType, docTitle, category as categoryId, o.contentId, o.fileId, CAST((fileSize/1024) AS float) as fileSize, fileExt, docDescription, publishDate, unpublishDate, case when o.publishDate is not null and o.unpublishDate is not null and @todayDate between o.publishDate and o.unpublishDate then 'Y' when o.publishDate is not null and o.unpublishDate is null and o.publishDate <= @todayDate then 'Y' when o.publishDate is not null and o.publishDate > @todayDate then 'F' when o.publishDate is null then 'N' else 'N' end isPublished, uploadDate, updateDate, updateUserId, case when restricted = 'Y' then 1 else 0 end restricted, -1 thumbnailId, -1 highresCntId, sortRank, coalesce( dateDiff(day, updateDate, @todayDate),0) as freshness, 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr , cast(concat('POS',':',o.fileId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , o.internalOnly, o.recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds from tbl_POS_Repository o with (nolock) left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('POS',':',o.fileId) where o.ownerId = @ownerId and o.ownerType = @ownerType union all select 'PRD' contentDomain, containerId, containerType, docTitle, category as categoryId, o.contentId, o.fileId, CAST((fileSize/1024) AS float) as fileSize, fileExt, docDescription, publishDate, unpublishDate, case when o.publishDate is not null and o.unpublishDate is not null and @todayDate between o.publishDate and o.unpublishDate then 'Y' when o.publishDate is not null and o.unpublishDate is null and o.publishDate <= @todayDate then 'Y' when o.publishDate is not null and o.publishDate > @todayDate then 'F' when o.publishDate is null then 'N' else 'N' end isPublished, uploadDate, updateDate, updateUserId, case when restricted = 'Y' then 1 else 0 end restricted, -1 thumbnailId, -1 highresCntId, sortRank, coalesce( dateDiff(day, updateDate, @todayDate),0) as freshness, 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr , cast(concat('PRD',':',o.fileId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , o.internalOnly, o.recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds from tbl_PRD_Repository o with (nolock) left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('PRD',':',o.fileId) where o.ownerId = @ownerId and o.ownerType = @ownerType union all select 'PRM' contentDomain, containerId, containerType, docTitle, category as categoryId, o.contentId, o.fileId, CAST((fileSize/1024) AS float) as fileSize, fileExt, docDescription, publishDate, unpublishDate, case when o.publishDate is not null and o.unpublishDate is not null and @todayDate between o.publishDate and o.unpublishDate then 'Y' when o.publishDate is not null and o.unpublishDate is null and o.publishDate <= @todayDate then 'Y' when o.publishDate is not null and o.publishDate > @todayDate then 'F' when o.publishDate is null then 'N' else 'N' end isPublished, uploadDate, updateDate, updateUserId, case when restricted = 'Y' then 1 else 0 end restricted, -1 thumbnailId, -1 highresCntId, sortRank, coalesce( dateDiff(day, updateDate, @todayDate),0) as freshness, 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr , cast(concat('PRM',':',o.fileId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , o.internalOnly, o.recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds from tbl_PRM_Repository o with (nolock) left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('PRM',':',o.fileId) where o.ownerId = @ownerId and o.ownerType = @ownerType union all select 'PRT' contentDomain, containerId, containerType, docTitle, category as categoryId, o.contentId, o.fileId, CAST((fileSize/1024) AS float) as fileSize, fileExt, docDescription, publishDate, unpublishDate, case when o.publishDate is not null and o.unpublishDate is not null and @todayDate between o.publishDate and o.unpublishDate then 'Y' when o.publishDate is not null and o.unpublishDate is null and o.publishDate <= @todayDate then 'Y' when o.publishDate is not null and o.publishDate > @todayDate then 'F' when o.publishDate is null then 'N' else 'N' end isPublished, uploadDate, updateDate, updateUserId, case when restricted = 'Y' then 1 else 0 end restricted, -1 thumbnailId, -1 highresCntId, sortRank, coalesce( dateDiff(day, updateDate, @todayDate),0) as freshness, 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr , cast(concat('PRT',':',o.fileId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , o.internalOnly, o.recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds from tbl_PRT_Repository o with (nolock) left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('PRT',':',o.fileId) where o.ownerId = @ownerId and o.ownerType = @ownerType union all select 'QA' contentDomain, containerId, containerType, docTitle, category as categoryId, o.contentId, o.fileId, CAST((fileSize/1024) AS float) as fileSize, fileExt, docDescription, publishDate, unpublishDate, case when o.publishDate is not null and o.unpublishDate is not null and @todayDate between o.publishDate and o.unpublishDate then 'Y' when o.publishDate is not null and o.unpublishDate is null and o.publishDate <= @todayDate then 'Y' when o.publishDate is not null and o.publishDate > @todayDate then 'F' when o.publishDate is null then 'N' else 'N' end isPublished, uploadDate, updateDate, updateUserId, case when restricted = 'Y' then 1 else 0 end restricted, -1 thumbnailId, -1 highresCntId, sortRank, coalesce( dateDiff(day, updateDate, @todayDate),0) as freshness, 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr , cast(concat('QA',':',o.fileId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , o.internalOnly, o.recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds from tbl_QA_Repository o with (nolock) left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('QA',':',o.fileId) where o.ownerId = @ownerId and o.ownerType = @ownerType union all select 'REC' contentDomain, containerId, containerType, docTitle, category as categoryId, o.contentId, o.fileId, CAST((fileSize/1024) AS float) as fileSize, fileExt, docDescription, publishDate, unpublishDate, case when o.publishDate is not null and o.unpublishDate is not null and @todayDate between o.publishDate and o.unpublishDate then 'Y' when o.publishDate is not null and o.unpublishDate is null and o.publishDate <= @todayDate then 'Y' when o.publishDate is not null and o.publishDate > @todayDate then 'F' when o.publishDate is null then 'N' else 'N' end isPublished, uploadDate, updateDate, updateUserId, case when restricted = 'Y' then 1 else 0 end restricted, -1 thumbnailId, -1 highresCntId, sortRank, coalesce( dateDiff(day, updateDate, @todayDate),0) as freshness, 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr , cast(concat('REC',':',o.fileId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , o.internalOnly, o.recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds from tbl_REC_Repository o with (nolock) left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('REC',':',o.fileId) where o.ownerId = @ownerId and o.ownerType = @ownerType union all select 'SCRUM' contentDomain, containerId, containerType, docTitle, category as categoryId, o.contentId, o.fileId, CAST((fileSize/1024) AS float) as fileSize, fileExt, docDescription, publishDate, unpublishDate, case when o.publishDate is not null and o.unpublishDate is not null and @todayDate between o.publishDate and o.unpublishDate then 'Y' when o.publishDate is not null and o.unpublishDate is null and o.publishDate <= @todayDate then 'Y' when o.publishDate is not null and o.publishDate > @todayDate then 'F' when o.publishDate is null then 'N' else 'N' end isPublished, uploadDate, updateDate, updateUserId, case when restricted = 'Y' then 1 else 0 end restricted, -1 thumbnailId, -1 highresCntId, sortRank, coalesce( dateDiff(day, updateDate, @todayDate),0) as freshness, 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr , cast(concat('SCRUM',':',o.fileId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , o.internalOnly, o.recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds from tbl_SCRUM_Repository o with (nolock) left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('SCRUM',':',o.fileId) where o.ownerId = @ownerId and o.ownerType = @ownerType union all select 'SME' contentDomain, containerId, containerType, docTitle, category as categoryId, o.contentId, o.fileId, CAST((fileSize/1024) AS float) as fileSize, fileExt, docDescription, publishDate, unpublishDate, case when o.publishDate is not null and o.unpublishDate is not null and @todayDate between o.publishDate and o.unpublishDate then 'Y' when o.publishDate is not null and o.unpublishDate is null and o.publishDate <= @todayDate then 'Y' when o.publishDate is not null and o.publishDate > @todayDate then 'F' when o.publishDate is null then 'N' else 'N' end isPublished, uploadDate, updateDate, updateUserId, case when restricted = 'Y' then 1 else 0 end restricted, -1 thumbnailId, -1 highresCntId, sortRank, coalesce( dateDiff(day, updateDate, @todayDate),0) as freshness, 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr , cast(concat('SME',':',o.fileId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , o.internalOnly, o.recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds from tbl_SME_Repository o with (nolock) left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('SME',':',o.fileId) where o.ownerId = @ownerId and o.ownerType = @ownerType union all select 'SSO' contentDomain, containerId, containerType, docTitle, category as categoryId, o.contentId, o.fileId, CAST((fileSize/1024) AS float) as fileSize, fileExt, docDescription, publishDate, unpublishDate, case when o.publishDate is not null and o.unpublishDate is not null and @todayDate between o.publishDate and o.unpublishDate then 'Y' when o.publishDate is not null and o.unpublishDate is null and o.publishDate <= @todayDate then 'Y' when o.publishDate is not null and o.publishDate > @todayDate then 'F' when o.publishDate is null then 'N' else 'N' end isPublished, uploadDate, updateDate, updateUserId, case when restricted = 'Y' then 1 else 0 end restricted, -1 thumbnailId, -1 highresCntId, sortRank, coalesce( dateDiff(day, updateDate, @todayDate),0) as freshness, 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr , cast(concat('SSO',':',o.fileId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , o.internalOnly, o.recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds from tbl_SSO_Repository o with (nolock) left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('SSO',':',o.fileId) where o.ownerId = @ownerId and o.ownerType = @ownerType union all select 'STP' contentDomain, containerId, containerType, docTitle, category as categoryId, o.contentId, o.fileId, CAST((fileSize/1024) AS float) as fileSize, fileExt, docDescription, publishDate, unpublishDate, case when o.publishDate is not null and o.unpublishDate is not null and @todayDate between o.publishDate and o.unpublishDate then 'Y' when o.publishDate is not null and o.unpublishDate is null and o.publishDate <= @todayDate then 'Y' when o.publishDate is not null and o.publishDate > @todayDate then 'F' when o.publishDate is null then 'N' else 'N' end isPublished, uploadDate, updateDate, updateUserId, case when restricted = 'Y' then 1 else 0 end restricted, -1 thumbnailId, -1 highresCntId, sortRank, coalesce( dateDiff(day, updateDate, @todayDate),0) as freshness, 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr , cast(concat('STP',':',o.fileId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , o.internalOnly, o.recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds from tbl_STP_Repository o with (nolock) left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('STP',':',o.fileId) where o.ownerId = @ownerId and o.ownerType = @ownerType union all select 'TER' contentDomain, containerId, containerType, docTitle, category as categoryId, o.contentId, o.fileId, CAST((fileSize/1024) AS float) as fileSize, fileExt, docDescription, publishDate, unpublishDate, case when o.publishDate is not null and o.unpublishDate is not null and @todayDate between o.publishDate and o.unpublishDate then 'Y' when o.publishDate is not null and o.unpublishDate is null and o.publishDate <= @todayDate then 'Y' when o.publishDate is not null and o.publishDate > @todayDate then 'F' when o.publishDate is null then 'N' else 'N' end isPublished, uploadDate, updateDate, updateUserId, case when restricted = 'Y' then 1 else 0 end restricted, -1 thumbnailId, -1 highresCntId, sortRank, coalesce( dateDiff(day, updateDate, @todayDate),0) as freshness, 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr , cast(concat('TER',':',o.fileId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , o.internalOnly, o.recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds from tbl_TER_Repository o with (nolock) left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('TER',':',o.fileId) where o.ownerId = @ownerId and o.ownerType = @ownerType union all select 'TKT' contentDomain, containerId, containerType, docTitle, category as categoryId, o.contentId, o.fileId, CAST((fileSize/1024) AS float) as fileSize, fileExt, docDescription, publishDate, unpublishDate, case when o.publishDate is not null and o.unpublishDate is not null and @todayDate between o.publishDate and o.unpublishDate then 'Y' when o.publishDate is not null and o.unpublishDate is null and o.publishDate <= @todayDate then 'Y' when o.publishDate is not null and o.publishDate > @todayDate then 'F' when o.publishDate is null then 'N' else 'N' end isPublished, uploadDate, updateDate, updateUserId, case when restricted = 'Y' then 1 else 0 end restricted, -1 thumbnailId, -1 highresCntId, sortRank, coalesce( dateDiff(day, updateDate, @todayDate),0) as freshness, 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr , cast(concat('TKT',':',o.fileId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , o.internalOnly, o.recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds from tbl_TKT_Repository o with (nolock) left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('TKT',':',o.fileId) where o.ownerId = @ownerId and o.ownerType = @ownerType union all select 'TPM' contentDomain, containerId, containerType, docTitle, category as categoryId, o.contentId, o.fileId, CAST((fileSize/1024) AS float) as fileSize, fileExt, docDescription, publishDate, unpublishDate, case when o.publishDate is not null and o.unpublishDate is not null and @todayDate between o.publishDate and o.unpublishDate then 'Y' when o.publishDate is not null and o.unpublishDate is null and o.publishDate <= @todayDate then 'Y' when o.publishDate is not null and o.publishDate > @todayDate then 'F' when o.publishDate is null then 'N' else 'N' end isPublished, uploadDate, updateDate, updateUserId, case when restricted = 'Y' then 1 else 0 end restricted, -1 thumbnailId, -1 highresCntId, sortRank, coalesce( dateDiff(day, updateDate, @todayDate),0) as freshness, 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr , cast(concat('TPM',':',o.fileId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , o.internalOnly, o.recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds from tbl_TPM_Repository o with (nolock) left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('TPM',':',o.fileId) where o.ownerId = @ownerId and o.ownerType = @ownerType union all select 'TRN' contentDomain, containerId, containerType, docTitle, category as categoryId, o.contentId, o.fileId, CAST((fileSize/1024) AS float) as fileSize, fileExt, docDescription, publishDate, unpublishDate, case when o.publishDate is not null and o.unpublishDate is not null and @todayDate between o.publishDate and o.unpublishDate then 'Y' when o.publishDate is not null and o.unpublishDate is null and o.publishDate <= @todayDate then 'Y' when o.publishDate is not null and o.publishDate > @todayDate then 'F' when o.publishDate is null then 'N' else 'N' end isPublished, uploadDate, updateDate, updateUserId, case when restricted = 'Y' then 1 else 0 end restricted, -1 thumbnailId, -1 highresCntId, sortRank, coalesce( dateDiff(day, updateDate, @todayDate),0) as freshness, 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr , cast(concat('TRN',':',o.fileId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , o.internalOnly, o.recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds from tbl_TRN_Repository o with (nolock) left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('TRN',':',o.fileId) where o.ownerId = @ownerId and o.ownerType = @ownerType union all select 'WRK' contentDomain, containerId, containerType, docTitle, category as categoryId, o.contentId, o.fileId, CAST((fileSize/1024) AS float) as fileSize, fileExt, docDescription, publishDate, unpublishDate, case when o.publishDate is not null and o.unpublishDate is not null and @todayDate between o.publishDate and o.unpublishDate then 'Y' when o.publishDate is not null and o.unpublishDate is null and o.publishDate <= @todayDate then 'Y' when o.publishDate is not null and o.publishDate > @todayDate then 'F' when o.publishDate is null then 'N' else 'N' end isPublished, uploadDate, updateDate, updateUserId, case when restricted = 'Y' then 1 else 0 end restricted, -1 thumbnailId, -1 highresCntId, sortRank, coalesce( dateDiff(day, updateDate, @todayDate),0) as freshness, 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr , cast(concat('WRK',':',o.fileId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , o.internalOnly, o.recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds from tbl_WRK_Repository o with (nolock) left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('WRK',':',o.fileId) where o.ownerId = @ownerId and o.ownerType = @ownerType ), filteredRawAssets as ( select ra.* from rawAssets ra where (1=1 or ra.linked_brokerIds in (?) ) and assetSelectionKey = ? ) , assetsWithContentURL1 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL, c.fileName as docFileName, lub.firstname + ' ' + lub.lastname as lastUpdatedByFullName from filteredRawAssets ra LEFT OUTER JOIN tbl_FSPRO_Members lub with (nolock) ON ra.updateUserId = lub.fspro_userId inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) , assetsWithContentURL2 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as thumbnailURL, c.fileName as thnFileName from assetsWithContentURL1 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.thumbnailId and c.contentId != 755 ) , assetsWithContentURL3 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as highresURL, c.fileName as highresCntFileName from assetsWithContentURL2 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.highresCntId and c.contentId != 755 ) , assets as ( select a.*, case when right( a.contentURL, charindex( '.', reverse( a.contentURL )) - 1 ) in ( 'BMP','GIF','JPEG','JPG','PNG','TIF','TIFF','WBMP' ) then left( a.contentURL, len( a.contentURL ) - charindex( '.', reverse( a.contentURL ) )) + '_50' + right( a.contentURL, charindex( '.', reverse( a.contentURL )) ) else null end previewURL from assetsWithContentURL3 a ) , categories as ( select 'OPR' contentDomain, categoryId, categoryDescription from tbl_OPR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'CDR' contentDomain, categoryId, categoryDescription from tbl_CDR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'BPL' contentDomain, categoryId, categoryDescription from tbl_BPL_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'DOC' contentDomain, categoryId, categoryName as categoryDescription from tbl_DOC_Categories with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType ) , assetsWithCategory as ( select a.*, c.categoryDescription from assets a left outer join categories c on c.contentDomain = a.contentDomain and c.categoryId = a.categoryId ) ,industryCategories as ( SELECT categoryId, categoryDescription FROM tbl_PRD_RepositoryCategories with (nolock) WHERE containerType = 'SKU' AND ownerID = 0 AND ownerType = '*' AND left(categoryDescription, 3) <> 'GS1' ) , assetsWithIndustryCategory as ( select a.*, ic.categoryDescription as industryCategoryDescription from assetsWithCategory a left outer join industryCategories ic on ic.categoryId = a.categoryId ) , assetsWithPartner as ( select a.*, opr.companyName as partnerName, a.containerId as partnerId, a.containerType as partnerType, opr.territoryId from assetsWithIndustryCategory a inner join tbl_OPR_CLientOperators opr with (nolock) on opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = a.containerId where a.containerType = 'OPR' union all select a.*, cdr.cdr_dstName as partnerName, a.containerId as partnerId, a.containerType as partnerType, cdr.cdr_territoryId as territoryId from assetsWithIndustryCategory a inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = @ownerId and cdr.fsl_tablecode = @ownerType and cdr.cdr_recordId = a.containerId where a.containerType = 'CDR' union all select a.*, mfr.mfr_name as partnerName, mfr.mfr_id as partnerId, 'MFR' as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a inner join tbl_BRO_Principals bpl with (nolock) on bpl.ownerId = @ownerId and bpl.ownerType = @ownerType and bpl.principalId = a.containerId inner join tblManufacturers mfr with (nolock) on mfr.mfr_id = bpl.mfrId where a.containerType = 'BPL' union all select a.*,cast (null as varchar ) as partnerName, cast( null as integer ) as partnerId, cast( null as varchar ) as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a where coalesce( a.containerType, '???') not in ( 'BPL', 'OPR', 'CDR' ) ), assetsWithUser as ( select a.*, m.email updateUserEmail, m.firstName + ' ' + m.lastName as updateUserFullName, case when len(isnull(a.linked_prd,'')) > 0 or len(isnull(a.linked_sku,'')) > 0 then 'Y' else 'N' end as prdLink, case when len(isnull(a.linked_opr,'')) > 0 then 'Y' else 'N' end as oprLink, case when len(isnull(a.linked_cdr,'')) > 0 then 'Y' else 'N' end as cdrLink, case when len(isnull(a.linked_mfr,'')) > 0 then 'Y' else 'N' end as mfrLink from assetsWithPartner as a left outer join tbl_Fspro_members m with (nolock) on m.ownerId > 0 and m.fspro_userId = a.updateUserId ) , assetsWithUser_mappedData as ( select * from assetsWithUser ) , assetsWithUser_preData as ( select * from assetsWithUser_mappedData with (nolock) where ( 1 = 1 ) ) , assetsWithUser_data as ( select * from assetsWithUser_preData ) , assetsWithUser_return as ( select * from assetsWithUser_data where 1 = 1 order by docTitle offset 0 rows fetch next 10000000 rows only ) , assetsWithUser_result as ( select ( select count(*) from assetsWithUser_data ) as totalCount, ( select * from assetsWithUser_return for json auto, include_null_values ) as data ) select * from assetsWithUser_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER -- 2026-04-16 12:55:31.296 -- 0 -- DOC:181981 {ts '2026-04-16 12:55:39'} SQL declare @ownerId int = 207 ; declare @ownerType varchar(3) = 'MFR' ; declare @contentPrefix varchar(128) = 'http://dev.fsenablers.com/FSLContent/' ; declare @todayDate datetime = ?; with docCategories as ( SELECT cats.attrID as categoryId, cats.attrDescription AS categoryName FROM tbl_ORG_Attributes cats with (nolock) WHERE cats.OwnerID = @ownerId AND cats.FSL_TableCode = @ownerType AND cats.attrTypeID = 31 ), rawAssets as ( select null contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, cast( null as varchar ) docTitle, cast( null as int) as categoryId, cast( null as int ) contentId, cast( null as int) fileId, 0.00 fileSize, null fileExt, null docDescription, cast( null as datetime ) publishDate, cast( null as datetime ) unpublishDate, 'N' isPublished, cast( null as datetime ) uploadDate, cast( null as datetime ) updateDate, cast( null as int ) updateUserId, 0 restricted, -1 thumbnailId, -1 highresCntId, 99 as sortRank , 0 as freshness , 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr, cast( null as varchar ) as assetSelectionKey, 0 as selectedRecord , 'N' as internalOnly, '' as recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds union all select 'DOC' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, d.docTitle, d.repCategoryId as categoryId, d.contentId, d.docId as fileId, CAST((d.fileSize/1024) AS float) as fileSize , d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, case when d.publishDate is not null and d.publishDate > @todayDate then 'F' when d.publishDate is not null and d.unpublishDate is not null and @todayDate >= d.publishDate and @todayDate <= d.unpublishDate then 'Y' when d.publishDate is not null and d.unpublishDate is null and d.publishDate <= @todayDate then 'Y' when d.publishDate is null then 'N' else 'N' end isPublished, d.uploadDate, d.lastUpdatedDate updateDate, d.lastUpdatedBy updateUserId, 0 restricted, thumbnailId, -1 highresCntId, sortRank , coalesce(dateDiff(day, d.lastUpdatedDate, @todayDate),0) as freshness, case when d.privateYesNo = 1 then 'Y' else 'N' end privateYesNo, ou.firstname + ' ' + ou.lastname as uploadedByFullName, d.keyWords, d.unpublishToDo, d.internalNotes , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END), ',') AS linked_opr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END), ',') AS linked_prd , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END), ',') AS linked_sku , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END), ',') AS linked_cdr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END), ',') AS linked_mfr , cast(concat('DOC',':',d.docId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , d.internalOnly, d.recordSource , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 23 THEN li.child_attrID ELSE NULL END), ',') AS permissionGrpIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 7 THEN li.child_attrID ELSE NULL END), ',') AS linked_brokerIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 31 THEN li.child_attrID ELSE NULL END), ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = d.docId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 34 THEN li.child_attrID ELSE NULL END), ',') AS linked_UserMemberIds from tbl_DOC_Documents d with (nolock) LEFT OUTER JOIN tbl_FSPRO_Members ou with (nolock) ON d.AuthorUserId = ou.fspro_userId left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = d.docId and li.child_attrTypeID in (41,59,60,63,42,23,7,31,34) and li.mstr_attrTypeID = 30 left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('DOC',':',d.docId) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType GROUP BY d.docTitle, d.repCategoryId, d.contentId, d.docId,d.fileSize, d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, d.uploadDate, d.lastUpdatedDate, d.lastUpdatedBy,thumbnailId, sortRank , d.privateYesNo , ou.firstname,ou.lastname, d.keyWords, d.unpublishToDo, d.internalNotes,ast.assetSelectionKey, d.internalOnly, d.recordSource union all select 'IMG' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, i.imgTitle as docTitle, i.categoryId, i.contentId, i.imgId as fileId, CAST((i.fileSize/1024) AS float) as fileSize, i.fileExt, i.imgDescription as docDescription, i.publishDate, i.unpublishDate, case when i.publishDate is not null and i.publishDate > @todayDate then 'F' when i.publishDate is not null and i.unpublishDate is not null and @todayDate >= i.publishDate and @todayDate <= i.unpublishDate then 'Y' when i.publishDate is not null and i.unpublishDate is null and i.publishDate <= @todayDate then 'Y' when i.publishDate is null then 'N' else 'N' end isPublished, i.uploadDate, i.lastUpdatedDate updateDate, i.lastUpdatedBy updateUserId, 0 restricted, -1 thumbnailId, i.highresCntId, i.sortRank, coalesce(dateDiff(day, lastUpdatedDate, @todayDate),0) as freshness, case when i.privateYesNo = 1 then 'Y' else 'N' end privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , STRING_AGG(CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END, ',') AS linked_opr , STRING_AGG(CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END, ',') AS linked_prd , STRING_AGG(CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END, ',') AS linked_sku , STRING_AGG(CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END, ',') AS linked_cdr , STRING_AGG(CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END, ',') AS linked_mfr , cast(concat('IMG',':',i.imgId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , i.internalOnly, i.recordSource , STRING_AGG(CASE WHEN c.child_attrTypeID = 23 THEN c.child_attrID ELSE NULL END, ',') AS permissionGrpIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 7 THEN c.child_attrID ELSE NULL END, ',') AS linked_brokerIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 31 THEN c.child_attrID ELSE NULL END, ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = i.imgId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CASE WHEN c.child_attrTypeID = 34 THEN c.child_attrID ELSE NULL END, ',') AS linked_UserMemberIds from tbl_IMG_Images i with (nolock) left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = i.imgId and li.child_attrTypeID in (41,59,60,63,42) and li.mstr_attrTypeID = 33 left outer JOIN tbl_ORG_AttributeLinks c ON c.mstr_attrID = i.imgId and c.child_attrTypeID in (23,7,31,34) and c.mstr_attrTypeID = 30 left outer join tbl_DOC_Categories dc with (nolock) ON c.child_attrTypeID = 31 AND dc.categoryId = c.child_attrID AND dc.ownerId = @ownerId AND dc.fsl_tablecode = @ownerType left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('IMG',':',i.imgId) where i.ownerId = @ownerId and i.fsl_tablecode = @ownerType GROUP BY i.imgTitle, i.CategoryID, i.contentId, i.imgId, i.fileSize, i.fileExt, i.imgDescription, i.publishDate, i.unpublishDate, i.uploadDate, i.lastUpdatedDate, i.lastUpdatedBy, i.highresCntId, i.sortRank, i.privateYesNo,ast.assetSelectionKey, i.internalOnly, i.recordSource ), filteredRawAssets as ( select ra.* from rawAssets ra where (1=1 or ra.linked_brokerIds in (?) ) ) , assetsWithContentURL1 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL, c.fileName as docFileName, lub.firstname + ' ' + lub.lastname as lastUpdatedByFullName from filteredRawAssets ra LEFT OUTER JOIN tbl_FSPRO_Members lub with (nolock) ON ra.updateUserId = lub.fspro_userId inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) , assetsWithContentURL2 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as thumbnailURL, c.fileName as thnFileName from assetsWithContentURL1 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.thumbnailId and c.contentId != 755 ) , assetsWithContentURL3 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as highresURL, c.fileName as highresCntFileName from assetsWithContentURL2 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.highresCntId and c.contentId != 755 ) , assets as ( select a.*, case when right( a.contentURL, charindex( '.', reverse( a.contentURL )) - 1 ) in ( 'BMP','GIF','JPEG','JPG','PNG','TIF','TIFF','WBMP' ) then left( a.contentURL, len( a.contentURL ) - charindex( '.', reverse( a.contentURL ) )) + '_50' + right( a.contentURL, charindex( '.', reverse( a.contentURL )) ) else null end previewURL from assetsWithContentURL3 a ) , categories as ( select 'OPR' contentDomain, categoryId, categoryDescription from tbl_OPR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'CDR' contentDomain, categoryId, categoryDescription from tbl_CDR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'BPL' contentDomain, categoryId, categoryDescription from tbl_BPL_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'DOC' contentDomain, categoryId, categoryName as categoryDescription from tbl_DOC_Categories with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType ) , assetsWithCategory as ( select a.*, c.categoryDescription from assets a left outer join categories c on c.contentDomain = a.contentDomain and c.categoryId = a.categoryId ) ,industryCategories as ( SELECT categoryId, categoryDescription FROM tbl_PRD_RepositoryCategories with (nolock) WHERE containerType = 'SKU' AND ownerID = 0 AND ownerType = '*' AND left(categoryDescription, 3) <> 'GS1' ) , assetsWithIndustryCategory as ( select a.*, ic.categoryDescription as industryCategoryDescription from assetsWithCategory a left outer join industryCategories ic on ic.categoryId = a.categoryId ) , assetsWithPartner as ( select a.*, opr.companyName as partnerName, a.containerId as partnerId, a.containerType as partnerType, opr.territoryId from assetsWithIndustryCategory a inner join tbl_OPR_CLientOperators opr with (nolock) on opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = a.containerId where a.containerType = 'OPR' union all select a.*, cdr.cdr_dstName as partnerName, a.containerId as partnerId, a.containerType as partnerType, cdr.cdr_territoryId as territoryId from assetsWithIndustryCategory a inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = @ownerId and cdr.fsl_tablecode = @ownerType and cdr.cdr_recordId = a.containerId where a.containerType = 'CDR' union all select a.*, mfr.mfr_name as partnerName, mfr.mfr_id as partnerId, 'MFR' as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a inner join tbl_BRO_Principals bpl with (nolock) on bpl.ownerId = @ownerId and bpl.ownerType = @ownerType and bpl.principalId = a.containerId inner join tblManufacturers mfr with (nolock) on mfr.mfr_id = bpl.mfrId where a.containerType = 'BPL' union all select a.*,cast (null as varchar ) as partnerName, cast( null as integer ) as partnerId, cast( null as varchar ) as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a where coalesce( a.containerType, '???') not in ( 'BPL', 'OPR', 'CDR' ) ), assetsWithUser as ( select a.*, m.email updateUserEmail, m.firstName + ' ' + m.lastName as updateUserFullName, case when len(isnull(a.linked_prd,'')) > 0 or len(isnull(a.linked_sku,'')) > 0 then 'Y' else 'N' end as prdLink, case when len(isnull(a.linked_opr,'')) > 0 then 'Y' else 'N' end as oprLink, case when len(isnull(a.linked_cdr,'')) > 0 then 'Y' else 'N' end as cdrLink, case when len(isnull(a.linked_mfr,'')) > 0 then 'Y' else 'N' end as mfrLink from assetsWithPartner as a left outer join tbl_Fspro_members m with (nolock) on m.ownerId > 0 and m.fspro_userId = a.updateUserId ) , assetsWithUser_mappedData as ( select * from assetsWithUser ) , assetsWithUser_preData as ( select * from assetsWithUser_mappedData with (nolock) where [assetSelectionKey] = 'DOC:181981' ) , assetsWithUser_data as ( select * from assetsWithUser_preData ) , assetsWithUser_return as ( select * from assetsWithUser_data where 1 = 1 order by docTitle offset 0 rows fetch next 10000000 rows only ) , assetsWithUser_result as ( select ( select count(*) from assetsWithUser_data ) as totalCount, ( select * from assetsWithUser_return for json auto, include_null_values ) as data ) select * from assetsWithUser_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER -- 2026-04-16 12:55:39.478 -- 0 {ts '2026-04-16 12:56:19'} SQL declare @ownerId int = 207 ; declare @ownerType varchar(3) = 'MFR' ; declare @contentPrefix varchar(128) = 'http://dev.fsenablers.com/FSLContent/' ; declare @todayDate datetime = ?; with docCategories as ( SELECT cats.attrID as categoryId, cats.attrDescription AS categoryName FROM tbl_ORG_Attributes cats with (nolock) WHERE cats.OwnerID = @ownerId AND cats.FSL_TableCode = @ownerType AND cats.attrTypeID = 31 ), rawAssets as ( select null contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, cast( null as varchar ) docTitle, cast( null as int) as categoryId, cast( null as int ) contentId, cast( null as int) fileId, 0.00 fileSize, null fileExt, null docDescription, cast( null as datetime ) publishDate, cast( null as datetime ) unpublishDate, 'N' isPublished, cast( null as datetime ) uploadDate, cast( null as datetime ) updateDate, cast( null as int ) updateUserId, 0 restricted, -1 thumbnailId, -1 highresCntId, 99 as sortRank , 0 as freshness , 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr, cast( null as varchar ) as assetSelectionKey, 0 as selectedRecord , 'N' as internalOnly, '' as recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds union all select 'DOC' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, d.docTitle, d.repCategoryId as categoryId, d.contentId, d.docId as fileId, CAST((d.fileSize/1024) AS float) as fileSize , d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, case when d.publishDate is not null and d.publishDate > @todayDate then 'F' when d.publishDate is not null and d.unpublishDate is not null and @todayDate >= d.publishDate and @todayDate <= d.unpublishDate then 'Y' when d.publishDate is not null and d.unpublishDate is null and d.publishDate <= @todayDate then 'Y' when d.publishDate is null then 'N' else 'N' end isPublished, d.uploadDate, d.lastUpdatedDate updateDate, d.lastUpdatedBy updateUserId, 0 restricted, thumbnailId, -1 highresCntId, sortRank , coalesce(dateDiff(day, d.lastUpdatedDate, @todayDate),0) as freshness, case when d.privateYesNo = 1 then 'Y' else 'N' end privateYesNo, ou.firstname + ' ' + ou.lastname as uploadedByFullName, d.keyWords, d.unpublishToDo, d.internalNotes , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END), ',') AS linked_opr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END), ',') AS linked_prd , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END), ',') AS linked_sku , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END), ',') AS linked_cdr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END), ',') AS linked_mfr , cast(concat('DOC',':',d.docId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , d.internalOnly, d.recordSource , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 23 THEN li.child_attrID ELSE NULL END), ',') AS permissionGrpIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 7 THEN li.child_attrID ELSE NULL END), ',') AS linked_brokerIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 31 THEN li.child_attrID ELSE NULL END), ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = d.docId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 34 THEN li.child_attrID ELSE NULL END), ',') AS linked_UserMemberIds from tbl_DOC_Documents d with (nolock) LEFT OUTER JOIN tbl_FSPRO_Members ou with (nolock) ON d.AuthorUserId = ou.fspro_userId left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = d.docId and li.child_attrTypeID in (41,59,60,63,42,23,7,31,34) and li.mstr_attrTypeID = 30 left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('DOC',':',d.docId) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType GROUP BY d.docTitle, d.repCategoryId, d.contentId, d.docId,d.fileSize, d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, d.uploadDate, d.lastUpdatedDate, d.lastUpdatedBy,thumbnailId, sortRank , d.privateYesNo , ou.firstname,ou.lastname, d.keyWords, d.unpublishToDo, d.internalNotes,ast.assetSelectionKey, d.internalOnly, d.recordSource union all select 'IMG' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, i.imgTitle as docTitle, i.categoryId, i.contentId, i.imgId as fileId, CAST((i.fileSize/1024) AS float) as fileSize, i.fileExt, i.imgDescription as docDescription, i.publishDate, i.unpublishDate, case when i.publishDate is not null and i.publishDate > @todayDate then 'F' when i.publishDate is not null and i.unpublishDate is not null and @todayDate >= i.publishDate and @todayDate <= i.unpublishDate then 'Y' when i.publishDate is not null and i.unpublishDate is null and i.publishDate <= @todayDate then 'Y' when i.publishDate is null then 'N' else 'N' end isPublished, i.uploadDate, i.lastUpdatedDate updateDate, i.lastUpdatedBy updateUserId, 0 restricted, -1 thumbnailId, i.highresCntId, i.sortRank, coalesce(dateDiff(day, lastUpdatedDate, @todayDate),0) as freshness, case when i.privateYesNo = 1 then 'Y' else 'N' end privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , STRING_AGG(CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END, ',') AS linked_opr , STRING_AGG(CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END, ',') AS linked_prd , STRING_AGG(CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END, ',') AS linked_sku , STRING_AGG(CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END, ',') AS linked_cdr , STRING_AGG(CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END, ',') AS linked_mfr , cast(concat('IMG',':',i.imgId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , i.internalOnly, i.recordSource , STRING_AGG(CASE WHEN c.child_attrTypeID = 23 THEN c.child_attrID ELSE NULL END, ',') AS permissionGrpIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 7 THEN c.child_attrID ELSE NULL END, ',') AS linked_brokerIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 31 THEN c.child_attrID ELSE NULL END, ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = i.imgId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CASE WHEN c.child_attrTypeID = 34 THEN c.child_attrID ELSE NULL END, ',') AS linked_UserMemberIds from tbl_IMG_Images i with (nolock) left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = i.imgId and li.child_attrTypeID in (41,59,60,63,42) and li.mstr_attrTypeID = 33 left outer JOIN tbl_ORG_AttributeLinks c ON c.mstr_attrID = i.imgId and c.child_attrTypeID in (23,7,31,34) and c.mstr_attrTypeID = 30 left outer join tbl_DOC_Categories dc with (nolock) ON c.child_attrTypeID = 31 AND dc.categoryId = c.child_attrID AND dc.ownerId = @ownerId AND dc.fsl_tablecode = @ownerType left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('IMG',':',i.imgId) where i.ownerId = @ownerId and i.fsl_tablecode = @ownerType GROUP BY i.imgTitle, i.CategoryID, i.contentId, i.imgId, i.fileSize, i.fileExt, i.imgDescription, i.publishDate, i.unpublishDate, i.uploadDate, i.lastUpdatedDate, i.lastUpdatedBy, i.highresCntId, i.sortRank, i.privateYesNo,ast.assetSelectionKey, i.internalOnly, i.recordSource ), filteredRawAssets as ( select ra.* from rawAssets ra where (1=1 or ra.linked_brokerIds in (?) ) ) , assetsWithContentURL1 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL, c.fileName as docFileName, lub.firstname + ' ' + lub.lastname as lastUpdatedByFullName from filteredRawAssets ra LEFT OUTER JOIN tbl_FSPRO_Members lub with (nolock) ON ra.updateUserId = lub.fspro_userId inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) , assetsWithContentURL2 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as thumbnailURL, c.fileName as thnFileName from assetsWithContentURL1 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.thumbnailId and c.contentId != 755 ) , assetsWithContentURL3 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as highresURL, c.fileName as highresCntFileName from assetsWithContentURL2 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.highresCntId and c.contentId != 755 ) , assets as ( select a.*, case when right( a.contentURL, charindex( '.', reverse( a.contentURL )) - 1 ) in ( 'BMP','GIF','JPEG','JPG','PNG','TIF','TIFF','WBMP' ) then left( a.contentURL, len( a.contentURL ) - charindex( '.', reverse( a.contentURL ) )) + '_50' + right( a.contentURL, charindex( '.', reverse( a.contentURL )) ) else null end previewURL from assetsWithContentURL3 a ) , categories as ( select 'OPR' contentDomain, categoryId, categoryDescription from tbl_OPR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'CDR' contentDomain, categoryId, categoryDescription from tbl_CDR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'BPL' contentDomain, categoryId, categoryDescription from tbl_BPL_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'DOC' contentDomain, categoryId, categoryName as categoryDescription from tbl_DOC_Categories with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType ) , assetsWithCategory as ( select a.*, c.categoryDescription from assets a left outer join categories c on c.contentDomain = a.contentDomain and c.categoryId = a.categoryId ) ,industryCategories as ( SELECT categoryId, categoryDescription FROM tbl_PRD_RepositoryCategories with (nolock) WHERE containerType = 'SKU' AND ownerID = 0 AND ownerType = '*' AND left(categoryDescription, 3) <> 'GS1' ) , assetsWithIndustryCategory as ( select a.*, ic.categoryDescription as industryCategoryDescription from assetsWithCategory a left outer join industryCategories ic on ic.categoryId = a.categoryId ) , assetsWithPartner as ( select a.*, opr.companyName as partnerName, a.containerId as partnerId, a.containerType as partnerType, opr.territoryId from assetsWithIndustryCategory a inner join tbl_OPR_CLientOperators opr with (nolock) on opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = a.containerId where a.containerType = 'OPR' union all select a.*, cdr.cdr_dstName as partnerName, a.containerId as partnerId, a.containerType as partnerType, cdr.cdr_territoryId as territoryId from assetsWithIndustryCategory a inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = @ownerId and cdr.fsl_tablecode = @ownerType and cdr.cdr_recordId = a.containerId where a.containerType = 'CDR' union all select a.*, mfr.mfr_name as partnerName, mfr.mfr_id as partnerId, 'MFR' as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a inner join tbl_BRO_Principals bpl with (nolock) on bpl.ownerId = @ownerId and bpl.ownerType = @ownerType and bpl.principalId = a.containerId inner join tblManufacturers mfr with (nolock) on mfr.mfr_id = bpl.mfrId where a.containerType = 'BPL' union all select a.*,cast (null as varchar ) as partnerName, cast( null as integer ) as partnerId, cast( null as varchar ) as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a where coalesce( a.containerType, '???') not in ( 'BPL', 'OPR', 'CDR' ) ), assetsWithUser as ( select a.*, m.email updateUserEmail, m.firstName + ' ' + m.lastName as updateUserFullName, case when len(isnull(a.linked_prd,'')) > 0 or len(isnull(a.linked_sku,'')) > 0 then 'Y' else 'N' end as prdLink, case when len(isnull(a.linked_opr,'')) > 0 then 'Y' else 'N' end as oprLink, case when len(isnull(a.linked_cdr,'')) > 0 then 'Y' else 'N' end as cdrLink, case when len(isnull(a.linked_mfr,'')) > 0 then 'Y' else 'N' end as mfrLink from assetsWithPartner as a left outer join tbl_Fspro_members m with (nolock) on m.ownerId > 0 and m.fspro_userId = a.updateUserId ) , assetsWithUser_mappedData as ( select * from assetsWithUser ) , assetsWithUser_preData as ( select * from assetsWithUser_mappedData with (nolock) where ( ( 1 = 1 ) and ( ( ( [isPublished] = 'Y' ) or ( [isPublished] = 'F' ) ) ) ) ) , assetsWithUser_data as ( select * from assetsWithUser_preData ) , assetsWithUser_return as ( select * from assetsWithUser_data where 1 = 1 order by docTitle offset 0 rows fetch next 50 rows only ) , assetsWithUser_result as ( select ( select count(*) from assetsWithUser_data ) as totalCount, ( select * from assetsWithUser_return for json auto, include_null_values ) as data ) select * from assetsWithUser_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER -- 2026-04-16 12:56:19.118 -- 0 {ts '2026-04-16 12:56:20'} SQL declare @ownerId int = 207 ; declare @ownerType varchar(3) = 'MFR' ; declare @contentPrefix varchar(128) = 'http://dev.fsenablers.com/FSLContent/' ; declare @todayDate datetime = ?; with docCategories as ( SELECT cats.attrID as categoryId, cats.attrDescription AS categoryName FROM tbl_ORG_Attributes cats with (nolock) WHERE cats.OwnerID = @ownerId AND cats.FSL_TableCode = @ownerType AND cats.attrTypeID = 31 ), rawAssets as ( select null contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, cast( null as varchar ) docTitle, cast( null as int) as categoryId, cast( null as int ) contentId, cast( null as int) fileId, 0.00 fileSize, null fileExt, null docDescription, cast( null as datetime ) publishDate, cast( null as datetime ) unpublishDate, 'N' isPublished, cast( null as datetime ) uploadDate, cast( null as datetime ) updateDate, cast( null as int ) updateUserId, 0 restricted, -1 thumbnailId, -1 highresCntId, 99 as sortRank , 0 as freshness , 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr, cast( null as varchar ) as assetSelectionKey, 0 as selectedRecord , 'N' as internalOnly, '' as recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds union all select 'DOC' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, d.docTitle, d.repCategoryId as categoryId, d.contentId, d.docId as fileId, CAST((d.fileSize/1024) AS float) as fileSize , d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, case when d.publishDate is not null and d.publishDate > @todayDate then 'F' when d.publishDate is not null and d.unpublishDate is not null and @todayDate >= d.publishDate and @todayDate <= d.unpublishDate then 'Y' when d.publishDate is not null and d.unpublishDate is null and d.publishDate <= @todayDate then 'Y' when d.publishDate is null then 'N' else 'N' end isPublished, d.uploadDate, d.lastUpdatedDate updateDate, d.lastUpdatedBy updateUserId, 0 restricted, thumbnailId, -1 highresCntId, sortRank , coalesce(dateDiff(day, d.lastUpdatedDate, @todayDate),0) as freshness, case when d.privateYesNo = 1 then 'Y' else 'N' end privateYesNo, ou.firstname + ' ' + ou.lastname as uploadedByFullName, d.keyWords, d.unpublishToDo, d.internalNotes , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END), ',') AS linked_opr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END), ',') AS linked_prd , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END), ',') AS linked_sku , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END), ',') AS linked_cdr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END), ',') AS linked_mfr , cast(concat('DOC',':',d.docId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , d.internalOnly, d.recordSource , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 23 THEN li.child_attrID ELSE NULL END), ',') AS permissionGrpIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 7 THEN li.child_attrID ELSE NULL END), ',') AS linked_brokerIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 31 THEN li.child_attrID ELSE NULL END), ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = d.docId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 34 THEN li.child_attrID ELSE NULL END), ',') AS linked_UserMemberIds from tbl_DOC_Documents d with (nolock) LEFT OUTER JOIN tbl_FSPRO_Members ou with (nolock) ON d.AuthorUserId = ou.fspro_userId left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = d.docId and li.child_attrTypeID in (41,59,60,63,42,23,7,31,34) and li.mstr_attrTypeID = 30 left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('DOC',':',d.docId) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType GROUP BY d.docTitle, d.repCategoryId, d.contentId, d.docId,d.fileSize, d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, d.uploadDate, d.lastUpdatedDate, d.lastUpdatedBy,thumbnailId, sortRank , d.privateYesNo , ou.firstname,ou.lastname, d.keyWords, d.unpublishToDo, d.internalNotes,ast.assetSelectionKey, d.internalOnly, d.recordSource union all select 'IMG' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, i.imgTitle as docTitle, i.categoryId, i.contentId, i.imgId as fileId, CAST((i.fileSize/1024) AS float) as fileSize, i.fileExt, i.imgDescription as docDescription, i.publishDate, i.unpublishDate, case when i.publishDate is not null and i.publishDate > @todayDate then 'F' when i.publishDate is not null and i.unpublishDate is not null and @todayDate >= i.publishDate and @todayDate <= i.unpublishDate then 'Y' when i.publishDate is not null and i.unpublishDate is null and i.publishDate <= @todayDate then 'Y' when i.publishDate is null then 'N' else 'N' end isPublished, i.uploadDate, i.lastUpdatedDate updateDate, i.lastUpdatedBy updateUserId, 0 restricted, -1 thumbnailId, i.highresCntId, i.sortRank, coalesce(dateDiff(day, lastUpdatedDate, @todayDate),0) as freshness, case when i.privateYesNo = 1 then 'Y' else 'N' end privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , STRING_AGG(CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END, ',') AS linked_opr , STRING_AGG(CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END, ',') AS linked_prd , STRING_AGG(CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END, ',') AS linked_sku , STRING_AGG(CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END, ',') AS linked_cdr , STRING_AGG(CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END, ',') AS linked_mfr , cast(concat('IMG',':',i.imgId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , i.internalOnly, i.recordSource , STRING_AGG(CASE WHEN c.child_attrTypeID = 23 THEN c.child_attrID ELSE NULL END, ',') AS permissionGrpIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 7 THEN c.child_attrID ELSE NULL END, ',') AS linked_brokerIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 31 THEN c.child_attrID ELSE NULL END, ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = i.imgId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CASE WHEN c.child_attrTypeID = 34 THEN c.child_attrID ELSE NULL END, ',') AS linked_UserMemberIds from tbl_IMG_Images i with (nolock) left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = i.imgId and li.child_attrTypeID in (41,59,60,63,42) and li.mstr_attrTypeID = 33 left outer JOIN tbl_ORG_AttributeLinks c ON c.mstr_attrID = i.imgId and c.child_attrTypeID in (23,7,31,34) and c.mstr_attrTypeID = 30 left outer join tbl_DOC_Categories dc with (nolock) ON c.child_attrTypeID = 31 AND dc.categoryId = c.child_attrID AND dc.ownerId = @ownerId AND dc.fsl_tablecode = @ownerType left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('IMG',':',i.imgId) where i.ownerId = @ownerId and i.fsl_tablecode = @ownerType GROUP BY i.imgTitle, i.CategoryID, i.contentId, i.imgId, i.fileSize, i.fileExt, i.imgDescription, i.publishDate, i.unpublishDate, i.uploadDate, i.lastUpdatedDate, i.lastUpdatedBy, i.highresCntId, i.sortRank, i.privateYesNo,ast.assetSelectionKey, i.internalOnly, i.recordSource ), filteredRawAssets as ( select ra.* from rawAssets ra where (1=1 or ra.linked_brokerIds in (?) ) ) , assetsWithContentURL1 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL, c.fileName as docFileName, lub.firstname + ' ' + lub.lastname as lastUpdatedByFullName from filteredRawAssets ra LEFT OUTER JOIN tbl_FSPRO_Members lub with (nolock) ON ra.updateUserId = lub.fspro_userId inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) , assetsWithContentURL2 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as thumbnailURL, c.fileName as thnFileName from assetsWithContentURL1 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.thumbnailId and c.contentId != 755 ) , assetsWithContentURL3 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as highresURL, c.fileName as highresCntFileName from assetsWithContentURL2 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.highresCntId and c.contentId != 755 ) , assets as ( select a.*, case when right( a.contentURL, charindex( '.', reverse( a.contentURL )) - 1 ) in ( 'BMP','GIF','JPEG','JPG','PNG','TIF','TIFF','WBMP' ) then left( a.contentURL, len( a.contentURL ) - charindex( '.', reverse( a.contentURL ) )) + '_50' + right( a.contentURL, charindex( '.', reverse( a.contentURL )) ) else null end previewURL from assetsWithContentURL3 a ) , categories as ( select 'OPR' contentDomain, categoryId, categoryDescription from tbl_OPR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'CDR' contentDomain, categoryId, categoryDescription from tbl_CDR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'BPL' contentDomain, categoryId, categoryDescription from tbl_BPL_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'DOC' contentDomain, categoryId, categoryName as categoryDescription from tbl_DOC_Categories with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType ) , assetsWithCategory as ( select a.*, c.categoryDescription from assets a left outer join categories c on c.contentDomain = a.contentDomain and c.categoryId = a.categoryId ) ,industryCategories as ( SELECT categoryId, categoryDescription FROM tbl_PRD_RepositoryCategories with (nolock) WHERE containerType = 'SKU' AND ownerID = 0 AND ownerType = '*' AND left(categoryDescription, 3) <> 'GS1' ) , assetsWithIndustryCategory as ( select a.*, ic.categoryDescription as industryCategoryDescription from assetsWithCategory a left outer join industryCategories ic on ic.categoryId = a.categoryId ) , assetsWithPartner as ( select a.*, opr.companyName as partnerName, a.containerId as partnerId, a.containerType as partnerType, opr.territoryId from assetsWithIndustryCategory a inner join tbl_OPR_CLientOperators opr with (nolock) on opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = a.containerId where a.containerType = 'OPR' union all select a.*, cdr.cdr_dstName as partnerName, a.containerId as partnerId, a.containerType as partnerType, cdr.cdr_territoryId as territoryId from assetsWithIndustryCategory a inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = @ownerId and cdr.fsl_tablecode = @ownerType and cdr.cdr_recordId = a.containerId where a.containerType = 'CDR' union all select a.*, mfr.mfr_name as partnerName, mfr.mfr_id as partnerId, 'MFR' as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a inner join tbl_BRO_Principals bpl with (nolock) on bpl.ownerId = @ownerId and bpl.ownerType = @ownerType and bpl.principalId = a.containerId inner join tblManufacturers mfr with (nolock) on mfr.mfr_id = bpl.mfrId where a.containerType = 'BPL' union all select a.*,cast (null as varchar ) as partnerName, cast( null as integer ) as partnerId, cast( null as varchar ) as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a where coalesce( a.containerType, '???') not in ( 'BPL', 'OPR', 'CDR' ) ), assetsWithUser as ( select a.*, m.email updateUserEmail, m.firstName + ' ' + m.lastName as updateUserFullName, case when len(isnull(a.linked_prd,'')) > 0 or len(isnull(a.linked_sku,'')) > 0 then 'Y' else 'N' end as prdLink, case when len(isnull(a.linked_opr,'')) > 0 then 'Y' else 'N' end as oprLink, case when len(isnull(a.linked_cdr,'')) > 0 then 'Y' else 'N' end as cdrLink, case when len(isnull(a.linked_mfr,'')) > 0 then 'Y' else 'N' end as mfrLink from assetsWithPartner as a left outer join tbl_Fspro_members m with (nolock) on m.ownerId > 0 and m.fspro_userId = a.updateUserId ) , assetsWithUser_mappedData as ( select * from assetsWithUser ) , assetsWithUser_preData as ( select * from assetsWithUser_mappedData with (nolock) where ( ( [industryCategoryDescription] = 'Menu / Recipe Ideas' ) and ( ( 1 = 1 ) and ( ( ( [isPublished] = 'Y' ) or ( [isPublished] = 'F' ) ) ) ) ) ) , assetsWithUser_data as ( select * from assetsWithUser_preData ) , assetsWithUser_return as ( select * from assetsWithUser_data where 1 = 1 order by docTitle offset 0 rows fetch next 50 rows only ) , assetsWithUser_result as ( select ( select count(*) from assetsWithUser_data ) as totalCount, ( select * from assetsWithUser_return for json auto, include_null_values ) as data ) select * from assetsWithUser_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER -- 2026-04-16 12:56:19.758 -- 0 {ts '2026-04-16 12:56:20'} SQL declare @ownerId int = 207 ; declare @ownerType varchar(3) = 'MFR' ; declare @contentPrefix varchar(128) = 'http://dev.fsenablers.com/FSLContent/' ; declare @todayDate datetime = ?; with docCategories as ( SELECT cats.attrID as categoryId, cats.attrDescription AS categoryName FROM tbl_ORG_Attributes cats with (nolock) WHERE cats.OwnerID = @ownerId AND cats.FSL_TableCode = @ownerType AND cats.attrTypeID = 31 ), rawAssets as ( select null contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, cast( null as varchar ) docTitle, cast( null as int) as categoryId, cast( null as int ) contentId, cast( null as int) fileId, 0.00 fileSize, null fileExt, null docDescription, cast( null as datetime ) publishDate, cast( null as datetime ) unpublishDate, 'N' isPublished, cast( null as datetime ) uploadDate, cast( null as datetime ) updateDate, cast( null as int ) updateUserId, 0 restricted, -1 thumbnailId, -1 highresCntId, 99 as sortRank , 0 as freshness , 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr, cast( null as varchar ) as assetSelectionKey, 0 as selectedRecord , 'N' as internalOnly, '' as recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds union all select 'DOC' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, d.docTitle, d.repCategoryId as categoryId, d.contentId, d.docId as fileId, CAST((d.fileSize/1024) AS float) as fileSize , d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, case when d.publishDate is not null and d.publishDate > @todayDate then 'F' when d.publishDate is not null and d.unpublishDate is not null and @todayDate >= d.publishDate and @todayDate <= d.unpublishDate then 'Y' when d.publishDate is not null and d.unpublishDate is null and d.publishDate <= @todayDate then 'Y' when d.publishDate is null then 'N' else 'N' end isPublished, d.uploadDate, d.lastUpdatedDate updateDate, d.lastUpdatedBy updateUserId, 0 restricted, thumbnailId, -1 highresCntId, sortRank , coalesce(dateDiff(day, d.lastUpdatedDate, @todayDate),0) as freshness, case when d.privateYesNo = 1 then 'Y' else 'N' end privateYesNo, ou.firstname + ' ' + ou.lastname as uploadedByFullName, d.keyWords, d.unpublishToDo, d.internalNotes , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END), ',') AS linked_opr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END), ',') AS linked_prd , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END), ',') AS linked_sku , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END), ',') AS linked_cdr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END), ',') AS linked_mfr , cast(concat('DOC',':',d.docId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , d.internalOnly, d.recordSource , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 23 THEN li.child_attrID ELSE NULL END), ',') AS permissionGrpIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 7 THEN li.child_attrID ELSE NULL END), ',') AS linked_brokerIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 31 THEN li.child_attrID ELSE NULL END), ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = d.docId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 34 THEN li.child_attrID ELSE NULL END), ',') AS linked_UserMemberIds from tbl_DOC_Documents d with (nolock) LEFT OUTER JOIN tbl_FSPRO_Members ou with (nolock) ON d.AuthorUserId = ou.fspro_userId left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = d.docId and li.child_attrTypeID in (41,59,60,63,42,23,7,31,34) and li.mstr_attrTypeID = 30 left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('DOC',':',d.docId) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType GROUP BY d.docTitle, d.repCategoryId, d.contentId, d.docId,d.fileSize, d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, d.uploadDate, d.lastUpdatedDate, d.lastUpdatedBy,thumbnailId, sortRank , d.privateYesNo , ou.firstname,ou.lastname, d.keyWords, d.unpublishToDo, d.internalNotes,ast.assetSelectionKey, d.internalOnly, d.recordSource union all select 'IMG' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, i.imgTitle as docTitle, i.categoryId, i.contentId, i.imgId as fileId, CAST((i.fileSize/1024) AS float) as fileSize, i.fileExt, i.imgDescription as docDescription, i.publishDate, i.unpublishDate, case when i.publishDate is not null and i.publishDate > @todayDate then 'F' when i.publishDate is not null and i.unpublishDate is not null and @todayDate >= i.publishDate and @todayDate <= i.unpublishDate then 'Y' when i.publishDate is not null and i.unpublishDate is null and i.publishDate <= @todayDate then 'Y' when i.publishDate is null then 'N' else 'N' end isPublished, i.uploadDate, i.lastUpdatedDate updateDate, i.lastUpdatedBy updateUserId, 0 restricted, -1 thumbnailId, i.highresCntId, i.sortRank, coalesce(dateDiff(day, lastUpdatedDate, @todayDate),0) as freshness, case when i.privateYesNo = 1 then 'Y' else 'N' end privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , STRING_AGG(CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END, ',') AS linked_opr , STRING_AGG(CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END, ',') AS linked_prd , STRING_AGG(CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END, ',') AS linked_sku , STRING_AGG(CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END, ',') AS linked_cdr , STRING_AGG(CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END, ',') AS linked_mfr , cast(concat('IMG',':',i.imgId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , i.internalOnly, i.recordSource , STRING_AGG(CASE WHEN c.child_attrTypeID = 23 THEN c.child_attrID ELSE NULL END, ',') AS permissionGrpIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 7 THEN c.child_attrID ELSE NULL END, ',') AS linked_brokerIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 31 THEN c.child_attrID ELSE NULL END, ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = i.imgId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CASE WHEN c.child_attrTypeID = 34 THEN c.child_attrID ELSE NULL END, ',') AS linked_UserMemberIds from tbl_IMG_Images i with (nolock) left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = i.imgId and li.child_attrTypeID in (41,59,60,63,42) and li.mstr_attrTypeID = 33 left outer JOIN tbl_ORG_AttributeLinks c ON c.mstr_attrID = i.imgId and c.child_attrTypeID in (23,7,31,34) and c.mstr_attrTypeID = 30 left outer join tbl_DOC_Categories dc with (nolock) ON c.child_attrTypeID = 31 AND dc.categoryId = c.child_attrID AND dc.ownerId = @ownerId AND dc.fsl_tablecode = @ownerType left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('IMG',':',i.imgId) where i.ownerId = @ownerId and i.fsl_tablecode = @ownerType GROUP BY i.imgTitle, i.CategoryID, i.contentId, i.imgId, i.fileSize, i.fileExt, i.imgDescription, i.publishDate, i.unpublishDate, i.uploadDate, i.lastUpdatedDate, i.lastUpdatedBy, i.highresCntId, i.sortRank, i.privateYesNo,ast.assetSelectionKey, i.internalOnly, i.recordSource ), filteredRawAssets as ( select ra.* from rawAssets ra where (1=1 or ra.linked_brokerIds in (?) ) ) , assetsWithContentURL1 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL, c.fileName as docFileName, lub.firstname + ' ' + lub.lastname as lastUpdatedByFullName from filteredRawAssets ra LEFT OUTER JOIN tbl_FSPRO_Members lub with (nolock) ON ra.updateUserId = lub.fspro_userId inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) , assetsWithContentURL2 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as thumbnailURL, c.fileName as thnFileName from assetsWithContentURL1 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.thumbnailId and c.contentId != 755 ) , assetsWithContentURL3 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as highresURL, c.fileName as highresCntFileName from assetsWithContentURL2 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.highresCntId and c.contentId != 755 ) , assets as ( select a.*, case when right( a.contentURL, charindex( '.', reverse( a.contentURL )) - 1 ) in ( 'BMP','GIF','JPEG','JPG','PNG','TIF','TIFF','WBMP' ) then left( a.contentURL, len( a.contentURL ) - charindex( '.', reverse( a.contentURL ) )) + '_50' + right( a.contentURL, charindex( '.', reverse( a.contentURL )) ) else null end previewURL from assetsWithContentURL3 a ) , categories as ( select 'OPR' contentDomain, categoryId, categoryDescription from tbl_OPR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'CDR' contentDomain, categoryId, categoryDescription from tbl_CDR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'BPL' contentDomain, categoryId, categoryDescription from tbl_BPL_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'DOC' contentDomain, categoryId, categoryName as categoryDescription from tbl_DOC_Categories with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType ) , assetsWithCategory as ( select a.*, c.categoryDescription from assets a left outer join categories c on c.contentDomain = a.contentDomain and c.categoryId = a.categoryId ) ,industryCategories as ( SELECT categoryId, categoryDescription FROM tbl_PRD_RepositoryCategories with (nolock) WHERE containerType = 'SKU' AND ownerID = 0 AND ownerType = '*' AND left(categoryDescription, 3) <> 'GS1' ) , assetsWithIndustryCategory as ( select a.*, ic.categoryDescription as industryCategoryDescription from assetsWithCategory a left outer join industryCategories ic on ic.categoryId = a.categoryId ) , assetsWithPartner as ( select a.*, opr.companyName as partnerName, a.containerId as partnerId, a.containerType as partnerType, opr.territoryId from assetsWithIndustryCategory a inner join tbl_OPR_CLientOperators opr with (nolock) on opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = a.containerId where a.containerType = 'OPR' union all select a.*, cdr.cdr_dstName as partnerName, a.containerId as partnerId, a.containerType as partnerType, cdr.cdr_territoryId as territoryId from assetsWithIndustryCategory a inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = @ownerId and cdr.fsl_tablecode = @ownerType and cdr.cdr_recordId = a.containerId where a.containerType = 'CDR' union all select a.*, mfr.mfr_name as partnerName, mfr.mfr_id as partnerId, 'MFR' as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a inner join tbl_BRO_Principals bpl with (nolock) on bpl.ownerId = @ownerId and bpl.ownerType = @ownerType and bpl.principalId = a.containerId inner join tblManufacturers mfr with (nolock) on mfr.mfr_id = bpl.mfrId where a.containerType = 'BPL' union all select a.*,cast (null as varchar ) as partnerName, cast( null as integer ) as partnerId, cast( null as varchar ) as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a where coalesce( a.containerType, '???') not in ( 'BPL', 'OPR', 'CDR' ) ), assetsWithUser as ( select a.*, m.email updateUserEmail, m.firstName + ' ' + m.lastName as updateUserFullName, case when len(isnull(a.linked_prd,'')) > 0 or len(isnull(a.linked_sku,'')) > 0 then 'Y' else 'N' end as prdLink, case when len(isnull(a.linked_opr,'')) > 0 then 'Y' else 'N' end as oprLink, case when len(isnull(a.linked_cdr,'')) > 0 then 'Y' else 'N' end as cdrLink, case when len(isnull(a.linked_mfr,'')) > 0 then 'Y' else 'N' end as mfrLink from assetsWithPartner as a left outer join tbl_Fspro_members m with (nolock) on m.ownerId > 0 and m.fspro_userId = a.updateUserId ) , assetsWithUser_mappedData as ( select * from assetsWithUser ) , assetsWithUser_preData as ( select * from assetsWithUser_mappedData with (nolock) where ( [assetSelectionKey] = 'DOC:158605' ) ) , assetsWithUser_data as ( select * from assetsWithUser_preData ) , assetsWithUser_return as ( select * from assetsWithUser_data where 1 = 1 order by docTitle offset 0 rows fetch next 10000000 rows only ) , assetsWithUser_result as ( select ( select count(*) from assetsWithUser_data ) as totalCount, ( select * from assetsWithUser_return for json auto, include_null_values ) as data ) select * from assetsWithUser_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER -- 2026-04-16 12:56:19.758 -- 0 {ts '2026-04-16 12:56:23'} SQL declare @ownerId int = 207 ; declare @ownerType varchar(3) = 'MFR' ; declare @contentPrefix varchar(128) = 'http://dev.fsenablers.com/FSLContent/' ; declare @todayDate datetime = ?; with docCategories as ( SELECT cats.attrID as categoryId, cats.attrDescription AS categoryName FROM tbl_ORG_Attributes cats with (nolock) WHERE cats.OwnerID = @ownerId AND cats.FSL_TableCode = @ownerType AND cats.attrTypeID = 31 ), rawAssets as ( select null contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, cast( null as varchar ) docTitle, cast( null as int) as categoryId, cast( null as int ) contentId, cast( null as int) fileId, 0.00 fileSize, null fileExt, null docDescription, cast( null as datetime ) publishDate, cast( null as datetime ) unpublishDate, 'N' isPublished, cast( null as datetime ) uploadDate, cast( null as datetime ) updateDate, cast( null as int ) updateUserId, 0 restricted, -1 thumbnailId, -1 highresCntId, 99 as sortRank , 0 as freshness , 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr, cast( null as varchar ) as assetSelectionKey, 0 as selectedRecord , 'N' as internalOnly, '' as recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds union all select 'DOC' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, d.docTitle, d.repCategoryId as categoryId, d.contentId, d.docId as fileId, CAST((d.fileSize/1024) AS float) as fileSize , d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, case when d.publishDate is not null and d.publishDate > @todayDate then 'F' when d.publishDate is not null and d.unpublishDate is not null and @todayDate >= d.publishDate and @todayDate <= d.unpublishDate then 'Y' when d.publishDate is not null and d.unpublishDate is null and d.publishDate <= @todayDate then 'Y' when d.publishDate is null then 'N' else 'N' end isPublished, d.uploadDate, d.lastUpdatedDate updateDate, d.lastUpdatedBy updateUserId, 0 restricted, thumbnailId, -1 highresCntId, sortRank , coalesce(dateDiff(day, d.lastUpdatedDate, @todayDate),0) as freshness, case when d.privateYesNo = 1 then 'Y' else 'N' end privateYesNo, ou.firstname + ' ' + ou.lastname as uploadedByFullName, d.keyWords, d.unpublishToDo, d.internalNotes , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END), ',') AS linked_opr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END), ',') AS linked_prd , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END), ',') AS linked_sku , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END), ',') AS linked_cdr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END), ',') AS linked_mfr , cast(concat('DOC',':',d.docId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , d.internalOnly, d.recordSource , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 23 THEN li.child_attrID ELSE NULL END), ',') AS permissionGrpIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 7 THEN li.child_attrID ELSE NULL END), ',') AS linked_brokerIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 31 THEN li.child_attrID ELSE NULL END), ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = d.docId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 34 THEN li.child_attrID ELSE NULL END), ',') AS linked_UserMemberIds from tbl_DOC_Documents d with (nolock) LEFT OUTER JOIN tbl_FSPRO_Members ou with (nolock) ON d.AuthorUserId = ou.fspro_userId left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = d.docId and li.child_attrTypeID in (41,59,60,63,42,23,7,31,34) and li.mstr_attrTypeID = 30 left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('DOC',':',d.docId) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType GROUP BY d.docTitle, d.repCategoryId, d.contentId, d.docId,d.fileSize, d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, d.uploadDate, d.lastUpdatedDate, d.lastUpdatedBy,thumbnailId, sortRank , d.privateYesNo , ou.firstname,ou.lastname, d.keyWords, d.unpublishToDo, d.internalNotes,ast.assetSelectionKey, d.internalOnly, d.recordSource union all select 'IMG' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, i.imgTitle as docTitle, i.categoryId, i.contentId, i.imgId as fileId, CAST((i.fileSize/1024) AS float) as fileSize, i.fileExt, i.imgDescription as docDescription, i.publishDate, i.unpublishDate, case when i.publishDate is not null and i.publishDate > @todayDate then 'F' when i.publishDate is not null and i.unpublishDate is not null and @todayDate >= i.publishDate and @todayDate <= i.unpublishDate then 'Y' when i.publishDate is not null and i.unpublishDate is null and i.publishDate <= @todayDate then 'Y' when i.publishDate is null then 'N' else 'N' end isPublished, i.uploadDate, i.lastUpdatedDate updateDate, i.lastUpdatedBy updateUserId, 0 restricted, -1 thumbnailId, i.highresCntId, i.sortRank, coalesce(dateDiff(day, lastUpdatedDate, @todayDate),0) as freshness, case when i.privateYesNo = 1 then 'Y' else 'N' end privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , STRING_AGG(CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END, ',') AS linked_opr , STRING_AGG(CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END, ',') AS linked_prd , STRING_AGG(CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END, ',') AS linked_sku , STRING_AGG(CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END, ',') AS linked_cdr , STRING_AGG(CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END, ',') AS linked_mfr , cast(concat('IMG',':',i.imgId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , i.internalOnly, i.recordSource , STRING_AGG(CASE WHEN c.child_attrTypeID = 23 THEN c.child_attrID ELSE NULL END, ',') AS permissionGrpIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 7 THEN c.child_attrID ELSE NULL END, ',') AS linked_brokerIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 31 THEN c.child_attrID ELSE NULL END, ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = i.imgId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CASE WHEN c.child_attrTypeID = 34 THEN c.child_attrID ELSE NULL END, ',') AS linked_UserMemberIds from tbl_IMG_Images i with (nolock) left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = i.imgId and li.child_attrTypeID in (41,59,60,63,42) and li.mstr_attrTypeID = 33 left outer JOIN tbl_ORG_AttributeLinks c ON c.mstr_attrID = i.imgId and c.child_attrTypeID in (23,7,31,34) and c.mstr_attrTypeID = 30 left outer join tbl_DOC_Categories dc with (nolock) ON c.child_attrTypeID = 31 AND dc.categoryId = c.child_attrID AND dc.ownerId = @ownerId AND dc.fsl_tablecode = @ownerType left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('IMG',':',i.imgId) where i.ownerId = @ownerId and i.fsl_tablecode = @ownerType GROUP BY i.imgTitle, i.CategoryID, i.contentId, i.imgId, i.fileSize, i.fileExt, i.imgDescription, i.publishDate, i.unpublishDate, i.uploadDate, i.lastUpdatedDate, i.lastUpdatedBy, i.highresCntId, i.sortRank, i.privateYesNo,ast.assetSelectionKey, i.internalOnly, i.recordSource ), filteredRawAssets as ( select ra.* from rawAssets ra where (1=1 or ra.linked_brokerIds in (?) ) ) , assetsWithContentURL1 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL, c.fileName as docFileName, lub.firstname + ' ' + lub.lastname as lastUpdatedByFullName from filteredRawAssets ra LEFT OUTER JOIN tbl_FSPRO_Members lub with (nolock) ON ra.updateUserId = lub.fspro_userId inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) , assetsWithContentURL2 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as thumbnailURL, c.fileName as thnFileName from assetsWithContentURL1 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.thumbnailId and c.contentId != 755 ) , assetsWithContentURL3 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as highresURL, c.fileName as highresCntFileName from assetsWithContentURL2 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.highresCntId and c.contentId != 755 ) , assets as ( select a.*, case when right( a.contentURL, charindex( '.', reverse( a.contentURL )) - 1 ) in ( 'BMP','GIF','JPEG','JPG','PNG','TIF','TIFF','WBMP' ) then left( a.contentURL, len( a.contentURL ) - charindex( '.', reverse( a.contentURL ) )) + '_50' + right( a.contentURL, charindex( '.', reverse( a.contentURL )) ) else null end previewURL from assetsWithContentURL3 a ) , categories as ( select 'OPR' contentDomain, categoryId, categoryDescription from tbl_OPR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'CDR' contentDomain, categoryId, categoryDescription from tbl_CDR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'BPL' contentDomain, categoryId, categoryDescription from tbl_BPL_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'DOC' contentDomain, categoryId, categoryName as categoryDescription from tbl_DOC_Categories with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType ) , assetsWithCategory as ( select a.*, c.categoryDescription from assets a left outer join categories c on c.contentDomain = a.contentDomain and c.categoryId = a.categoryId ) ,industryCategories as ( SELECT categoryId, categoryDescription FROM tbl_PRD_RepositoryCategories with (nolock) WHERE containerType = 'SKU' AND ownerID = 0 AND ownerType = '*' AND left(categoryDescription, 3) <> 'GS1' ) , assetsWithIndustryCategory as ( select a.*, ic.categoryDescription as industryCategoryDescription from assetsWithCategory a left outer join industryCategories ic on ic.categoryId = a.categoryId ) , assetsWithPartner as ( select a.*, opr.companyName as partnerName, a.containerId as partnerId, a.containerType as partnerType, opr.territoryId from assetsWithIndustryCategory a inner join tbl_OPR_CLientOperators opr with (nolock) on opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = a.containerId where a.containerType = 'OPR' union all select a.*, cdr.cdr_dstName as partnerName, a.containerId as partnerId, a.containerType as partnerType, cdr.cdr_territoryId as territoryId from assetsWithIndustryCategory a inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = @ownerId and cdr.fsl_tablecode = @ownerType and cdr.cdr_recordId = a.containerId where a.containerType = 'CDR' union all select a.*, mfr.mfr_name as partnerName, mfr.mfr_id as partnerId, 'MFR' as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a inner join tbl_BRO_Principals bpl with (nolock) on bpl.ownerId = @ownerId and bpl.ownerType = @ownerType and bpl.principalId = a.containerId inner join tblManufacturers mfr with (nolock) on mfr.mfr_id = bpl.mfrId where a.containerType = 'BPL' union all select a.*,cast (null as varchar ) as partnerName, cast( null as integer ) as partnerId, cast( null as varchar ) as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a where coalesce( a.containerType, '???') not in ( 'BPL', 'OPR', 'CDR' ) ), assetsWithUser as ( select a.*, m.email updateUserEmail, m.firstName + ' ' + m.lastName as updateUserFullName, case when len(isnull(a.linked_prd,'')) > 0 or len(isnull(a.linked_sku,'')) > 0 then 'Y' else 'N' end as prdLink, case when len(isnull(a.linked_opr,'')) > 0 then 'Y' else 'N' end as oprLink, case when len(isnull(a.linked_cdr,'')) > 0 then 'Y' else 'N' end as cdrLink, case when len(isnull(a.linked_mfr,'')) > 0 then 'Y' else 'N' end as mfrLink from assetsWithPartner as a left outer join tbl_Fspro_members m with (nolock) on m.ownerId > 0 and m.fspro_userId = a.updateUserId ) , assetsWithUser_mappedData as ( select * from assetsWithUser ) , assetsWithUser_preData as ( select * from assetsWithUser_mappedData with (nolock) where ( ( 1 = 1 ) and ( ( ( [isPublished] = 'Y' ) or ( [isPublished] = 'F' ) ) ) ) ) , assetsWithUser_data as ( select * from assetsWithUser_preData ) , assetsWithUser_return as ( select * from assetsWithUser_data where 1 = 1 order by docTitle offset 0 rows fetch next 50 rows only ) , assetsWithUser_result as ( select ( select count(*) from assetsWithUser_data ) as totalCount, ( select * from assetsWithUser_return for json auto, include_null_values ) as data ) select * from assetsWithUser_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER -- 2026-04-16 12:56:22.804 -- 0 {ts '2026-04-16 12:56:43'} SQL declare @ownerId int = 207 ; declare @ownerType varchar(3) = 'MFR' ; declare @contentPrefix varchar(128) = 'http://dev.fsenablers.com/FSLContent/' ; declare @todayDate datetime = ?; with docCategories as ( SELECT cats.attrID as categoryId, cats.attrDescription AS categoryName FROM tbl_ORG_Attributes cats with (nolock) WHERE cats.OwnerID = @ownerId AND cats.FSL_TableCode = @ownerType AND cats.attrTypeID = 31 ), rawAssets as ( select null contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, cast( null as varchar ) docTitle, cast( null as int) as categoryId, cast( null as int ) contentId, cast( null as int) fileId, 0.00 fileSize, null fileExt, null docDescription, cast( null as datetime ) publishDate, cast( null as datetime ) unpublishDate, 'N' isPublished, cast( null as datetime ) uploadDate, cast( null as datetime ) updateDate, cast( null as int ) updateUserId, 0 restricted, -1 thumbnailId, -1 highresCntId, 99 as sortRank , 0 as freshness , 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr, cast( null as varchar ) as assetSelectionKey, 0 as selectedRecord , 'N' as internalOnly, '' as recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds union all select 'DOC' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, d.docTitle, d.repCategoryId as categoryId, d.contentId, d.docId as fileId, CAST((d.fileSize/1024) AS float) as fileSize , d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, case when d.publishDate is not null and d.publishDate > @todayDate then 'F' when d.publishDate is not null and d.unpublishDate is not null and @todayDate >= d.publishDate and @todayDate <= d.unpublishDate then 'Y' when d.publishDate is not null and d.unpublishDate is null and d.publishDate <= @todayDate then 'Y' when d.publishDate is null then 'N' else 'N' end isPublished, d.uploadDate, d.lastUpdatedDate updateDate, d.lastUpdatedBy updateUserId, 0 restricted, thumbnailId, -1 highresCntId, sortRank , coalesce(dateDiff(day, d.lastUpdatedDate, @todayDate),0) as freshness, case when d.privateYesNo = 1 then 'Y' else 'N' end privateYesNo, ou.firstname + ' ' + ou.lastname as uploadedByFullName, d.keyWords, d.unpublishToDo, d.internalNotes , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END), ',') AS linked_opr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END), ',') AS linked_prd , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END), ',') AS linked_sku , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END), ',') AS linked_cdr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END), ',') AS linked_mfr , cast(concat('DOC',':',d.docId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , d.internalOnly, d.recordSource , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 23 THEN li.child_attrID ELSE NULL END), ',') AS permissionGrpIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 7 THEN li.child_attrID ELSE NULL END), ',') AS linked_brokerIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 31 THEN li.child_attrID ELSE NULL END), ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = d.docId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 34 THEN li.child_attrID ELSE NULL END), ',') AS linked_UserMemberIds from tbl_DOC_Documents d with (nolock) LEFT OUTER JOIN tbl_FSPRO_Members ou with (nolock) ON d.AuthorUserId = ou.fspro_userId left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = d.docId and li.child_attrTypeID in (41,59,60,63,42,23,7,31,34) and li.mstr_attrTypeID = 30 left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('DOC',':',d.docId) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType GROUP BY d.docTitle, d.repCategoryId, d.contentId, d.docId,d.fileSize, d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, d.uploadDate, d.lastUpdatedDate, d.lastUpdatedBy,thumbnailId, sortRank , d.privateYesNo , ou.firstname,ou.lastname, d.keyWords, d.unpublishToDo, d.internalNotes,ast.assetSelectionKey, d.internalOnly, d.recordSource union all select 'IMG' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, i.imgTitle as docTitle, i.categoryId, i.contentId, i.imgId as fileId, CAST((i.fileSize/1024) AS float) as fileSize, i.fileExt, i.imgDescription as docDescription, i.publishDate, i.unpublishDate, case when i.publishDate is not null and i.publishDate > @todayDate then 'F' when i.publishDate is not null and i.unpublishDate is not null and @todayDate >= i.publishDate and @todayDate <= i.unpublishDate then 'Y' when i.publishDate is not null and i.unpublishDate is null and i.publishDate <= @todayDate then 'Y' when i.publishDate is null then 'N' else 'N' end isPublished, i.uploadDate, i.lastUpdatedDate updateDate, i.lastUpdatedBy updateUserId, 0 restricted, -1 thumbnailId, i.highresCntId, i.sortRank, coalesce(dateDiff(day, lastUpdatedDate, @todayDate),0) as freshness, case when i.privateYesNo = 1 then 'Y' else 'N' end privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , STRING_AGG(CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END, ',') AS linked_opr , STRING_AGG(CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END, ',') AS linked_prd , STRING_AGG(CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END, ',') AS linked_sku , STRING_AGG(CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END, ',') AS linked_cdr , STRING_AGG(CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END, ',') AS linked_mfr , cast(concat('IMG',':',i.imgId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , i.internalOnly, i.recordSource , STRING_AGG(CASE WHEN c.child_attrTypeID = 23 THEN c.child_attrID ELSE NULL END, ',') AS permissionGrpIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 7 THEN c.child_attrID ELSE NULL END, ',') AS linked_brokerIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 31 THEN c.child_attrID ELSE NULL END, ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = i.imgId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CASE WHEN c.child_attrTypeID = 34 THEN c.child_attrID ELSE NULL END, ',') AS linked_UserMemberIds from tbl_IMG_Images i with (nolock) left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = i.imgId and li.child_attrTypeID in (41,59,60,63,42) and li.mstr_attrTypeID = 33 left outer JOIN tbl_ORG_AttributeLinks c ON c.mstr_attrID = i.imgId and c.child_attrTypeID in (23,7,31,34) and c.mstr_attrTypeID = 30 left outer join tbl_DOC_Categories dc with (nolock) ON c.child_attrTypeID = 31 AND dc.categoryId = c.child_attrID AND dc.ownerId = @ownerId AND dc.fsl_tablecode = @ownerType left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('IMG',':',i.imgId) where i.ownerId = @ownerId and i.fsl_tablecode = @ownerType GROUP BY i.imgTitle, i.CategoryID, i.contentId, i.imgId, i.fileSize, i.fileExt, i.imgDescription, i.publishDate, i.unpublishDate, i.uploadDate, i.lastUpdatedDate, i.lastUpdatedBy, i.highresCntId, i.sortRank, i.privateYesNo,ast.assetSelectionKey, i.internalOnly, i.recordSource ), filteredRawAssets as ( select ra.* from rawAssets ra where (1=1 or ra.linked_brokerIds in (?) ) ) , assetsWithContentURL1 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL, c.fileName as docFileName, lub.firstname + ' ' + lub.lastname as lastUpdatedByFullName from filteredRawAssets ra LEFT OUTER JOIN tbl_FSPRO_Members lub with (nolock) ON ra.updateUserId = lub.fspro_userId inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) , assetsWithContentURL2 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as thumbnailURL, c.fileName as thnFileName from assetsWithContentURL1 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.thumbnailId and c.contentId != 755 ) , assetsWithContentURL3 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as highresURL, c.fileName as highresCntFileName from assetsWithContentURL2 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.highresCntId and c.contentId != 755 ) , assets as ( select a.*, case when right( a.contentURL, charindex( '.', reverse( a.contentURL )) - 1 ) in ( 'BMP','GIF','JPEG','JPG','PNG','TIF','TIFF','WBMP' ) then left( a.contentURL, len( a.contentURL ) - charindex( '.', reverse( a.contentURL ) )) + '_50' + right( a.contentURL, charindex( '.', reverse( a.contentURL )) ) else null end previewURL from assetsWithContentURL3 a ) , categories as ( select 'OPR' contentDomain, categoryId, categoryDescription from tbl_OPR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'CDR' contentDomain, categoryId, categoryDescription from tbl_CDR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'BPL' contentDomain, categoryId, categoryDescription from tbl_BPL_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'DOC' contentDomain, categoryId, categoryName as categoryDescription from tbl_DOC_Categories with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType ) , assetsWithCategory as ( select a.*, c.categoryDescription from assets a left outer join categories c on c.contentDomain = a.contentDomain and c.categoryId = a.categoryId ) ,industryCategories as ( SELECT categoryId, categoryDescription FROM tbl_PRD_RepositoryCategories with (nolock) WHERE containerType = 'SKU' AND ownerID = 0 AND ownerType = '*' AND left(categoryDescription, 3) <> 'GS1' ) , assetsWithIndustryCategory as ( select a.*, ic.categoryDescription as industryCategoryDescription from assetsWithCategory a left outer join industryCategories ic on ic.categoryId = a.categoryId ) , assetsWithPartner as ( select a.*, opr.companyName as partnerName, a.containerId as partnerId, a.containerType as partnerType, opr.territoryId from assetsWithIndustryCategory a inner join tbl_OPR_CLientOperators opr with (nolock) on opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = a.containerId where a.containerType = 'OPR' union all select a.*, cdr.cdr_dstName as partnerName, a.containerId as partnerId, a.containerType as partnerType, cdr.cdr_territoryId as territoryId from assetsWithIndustryCategory a inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = @ownerId and cdr.fsl_tablecode = @ownerType and cdr.cdr_recordId = a.containerId where a.containerType = 'CDR' union all select a.*, mfr.mfr_name as partnerName, mfr.mfr_id as partnerId, 'MFR' as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a inner join tbl_BRO_Principals bpl with (nolock) on bpl.ownerId = @ownerId and bpl.ownerType = @ownerType and bpl.principalId = a.containerId inner join tblManufacturers mfr with (nolock) on mfr.mfr_id = bpl.mfrId where a.containerType = 'BPL' union all select a.*,cast (null as varchar ) as partnerName, cast( null as integer ) as partnerId, cast( null as varchar ) as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a where coalesce( a.containerType, '???') not in ( 'BPL', 'OPR', 'CDR' ) ), assetsWithUser as ( select a.*, m.email updateUserEmail, m.firstName + ' ' + m.lastName as updateUserFullName, case when len(isnull(a.linked_prd,'')) > 0 or len(isnull(a.linked_sku,'')) > 0 then 'Y' else 'N' end as prdLink, case when len(isnull(a.linked_opr,'')) > 0 then 'Y' else 'N' end as oprLink, case when len(isnull(a.linked_cdr,'')) > 0 then 'Y' else 'N' end as cdrLink, case when len(isnull(a.linked_mfr,'')) > 0 then 'Y' else 'N' end as mfrLink from assetsWithPartner as a left outer join tbl_Fspro_members m with (nolock) on m.ownerId > 0 and m.fspro_userId = a.updateUserId ) , assetsWithUser_mappedData as ( select * from assetsWithUser ) , assetsWithUser_preData as ( select * from assetsWithUser_mappedData with (nolock) where ( ( 1 = 1 ) and ( ( ( [isPublished] = 'Y' ) or ( [isPublished] = 'F' ) ) ) ) ) , assetsWithUser_data as ( select * from assetsWithUser_preData ) , assetsWithUser_return as ( select * from assetsWithUser_data where 1 = 1 order by docTitle offset 0 rows fetch next 50 rows only ) , assetsWithUser_result as ( select ( select count(*) from assetsWithUser_data ) as totalCount, ( select * from assetsWithUser_return for json auto, include_null_values ) as data ) select * from assetsWithUser_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER -- 2026-04-16 12:56:43.658 -- 0 {ts '2026-04-16 12:56:44'} SQL declare @ownerId int = 207 ; declare @ownerType varchar(3) = 'MFR' ; declare @contentPrefix varchar(128) = 'http://dev.fsenablers.com/FSLContent/' ; declare @todayDate datetime = ?; with docCategories as ( SELECT cats.attrID as categoryId, cats.attrDescription AS categoryName FROM tbl_ORG_Attributes cats with (nolock) WHERE cats.OwnerID = @ownerId AND cats.FSL_TableCode = @ownerType AND cats.attrTypeID = 31 ), rawAssets as ( select null contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, cast( null as varchar ) docTitle, cast( null as int) as categoryId, cast( null as int ) contentId, cast( null as int) fileId, 0.00 fileSize, null fileExt, null docDescription, cast( null as datetime ) publishDate, cast( null as datetime ) unpublishDate, 'N' isPublished, cast( null as datetime ) uploadDate, cast( null as datetime ) updateDate, cast( null as int ) updateUserId, 0 restricted, -1 thumbnailId, -1 highresCntId, 99 as sortRank , 0 as freshness , 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr, cast( null as varchar ) as assetSelectionKey, 0 as selectedRecord , 'N' as internalOnly, '' as recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds union all select 'DOC' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, d.docTitle, d.repCategoryId as categoryId, d.contentId, d.docId as fileId, CAST((d.fileSize/1024) AS float) as fileSize , d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, case when d.publishDate is not null and d.publishDate > @todayDate then 'F' when d.publishDate is not null and d.unpublishDate is not null and @todayDate >= d.publishDate and @todayDate <= d.unpublishDate then 'Y' when d.publishDate is not null and d.unpublishDate is null and d.publishDate <= @todayDate then 'Y' when d.publishDate is null then 'N' else 'N' end isPublished, d.uploadDate, d.lastUpdatedDate updateDate, d.lastUpdatedBy updateUserId, 0 restricted, thumbnailId, -1 highresCntId, sortRank , coalesce(dateDiff(day, d.lastUpdatedDate, @todayDate),0) as freshness, case when d.privateYesNo = 1 then 'Y' else 'N' end privateYesNo, ou.firstname + ' ' + ou.lastname as uploadedByFullName, d.keyWords, d.unpublishToDo, d.internalNotes , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END), ',') AS linked_opr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END), ',') AS linked_prd , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END), ',') AS linked_sku , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END), ',') AS linked_cdr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END), ',') AS linked_mfr , cast(concat('DOC',':',d.docId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , d.internalOnly, d.recordSource , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 23 THEN li.child_attrID ELSE NULL END), ',') AS permissionGrpIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 7 THEN li.child_attrID ELSE NULL END), ',') AS linked_brokerIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 31 THEN li.child_attrID ELSE NULL END), ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = d.docId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 34 THEN li.child_attrID ELSE NULL END), ',') AS linked_UserMemberIds from tbl_DOC_Documents d with (nolock) LEFT OUTER JOIN tbl_FSPRO_Members ou with (nolock) ON d.AuthorUserId = ou.fspro_userId left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = d.docId and li.child_attrTypeID in (41,59,60,63,42,23,7,31,34) and li.mstr_attrTypeID = 30 left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('DOC',':',d.docId) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType GROUP BY d.docTitle, d.repCategoryId, d.contentId, d.docId,d.fileSize, d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, d.uploadDate, d.lastUpdatedDate, d.lastUpdatedBy,thumbnailId, sortRank , d.privateYesNo , ou.firstname,ou.lastname, d.keyWords, d.unpublishToDo, d.internalNotes,ast.assetSelectionKey, d.internalOnly, d.recordSource union all select 'IMG' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, i.imgTitle as docTitle, i.categoryId, i.contentId, i.imgId as fileId, CAST((i.fileSize/1024) AS float) as fileSize, i.fileExt, i.imgDescription as docDescription, i.publishDate, i.unpublishDate, case when i.publishDate is not null and i.publishDate > @todayDate then 'F' when i.publishDate is not null and i.unpublishDate is not null and @todayDate >= i.publishDate and @todayDate <= i.unpublishDate then 'Y' when i.publishDate is not null and i.unpublishDate is null and i.publishDate <= @todayDate then 'Y' when i.publishDate is null then 'N' else 'N' end isPublished, i.uploadDate, i.lastUpdatedDate updateDate, i.lastUpdatedBy updateUserId, 0 restricted, -1 thumbnailId, i.highresCntId, i.sortRank, coalesce(dateDiff(day, lastUpdatedDate, @todayDate),0) as freshness, case when i.privateYesNo = 1 then 'Y' else 'N' end privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , STRING_AGG(CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END, ',') AS linked_opr , STRING_AGG(CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END, ',') AS linked_prd , STRING_AGG(CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END, ',') AS linked_sku , STRING_AGG(CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END, ',') AS linked_cdr , STRING_AGG(CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END, ',') AS linked_mfr , cast(concat('IMG',':',i.imgId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , i.internalOnly, i.recordSource , STRING_AGG(CASE WHEN c.child_attrTypeID = 23 THEN c.child_attrID ELSE NULL END, ',') AS permissionGrpIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 7 THEN c.child_attrID ELSE NULL END, ',') AS linked_brokerIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 31 THEN c.child_attrID ELSE NULL END, ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = i.imgId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CASE WHEN c.child_attrTypeID = 34 THEN c.child_attrID ELSE NULL END, ',') AS linked_UserMemberIds from tbl_IMG_Images i with (nolock) left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = i.imgId and li.child_attrTypeID in (41,59,60,63,42) and li.mstr_attrTypeID = 33 left outer JOIN tbl_ORG_AttributeLinks c ON c.mstr_attrID = i.imgId and c.child_attrTypeID in (23,7,31,34) and c.mstr_attrTypeID = 30 left outer join tbl_DOC_Categories dc with (nolock) ON c.child_attrTypeID = 31 AND dc.categoryId = c.child_attrID AND dc.ownerId = @ownerId AND dc.fsl_tablecode = @ownerType left outer join tmp_assetsSelections_MFR207__14045051 ast with (nolock) on ast.assetSelectionKey = concat('IMG',':',i.imgId) where i.ownerId = @ownerId and i.fsl_tablecode = @ownerType GROUP BY i.imgTitle, i.CategoryID, i.contentId, i.imgId, i.fileSize, i.fileExt, i.imgDescription, i.publishDate, i.unpublishDate, i.uploadDate, i.lastUpdatedDate, i.lastUpdatedBy, i.highresCntId, i.sortRank, i.privateYesNo,ast.assetSelectionKey, i.internalOnly, i.recordSource ), filteredRawAssets as ( select ra.* from rawAssets ra where (1=1 or ra.linked_brokerIds in (?) ) ) , assetsWithContentURL1 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL, c.fileName as docFileName, lub.firstname + ' ' + lub.lastname as lastUpdatedByFullName from filteredRawAssets ra LEFT OUTER JOIN tbl_FSPRO_Members lub with (nolock) ON ra.updateUserId = lub.fspro_userId inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) , assetsWithContentURL2 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as thumbnailURL, c.fileName as thnFileName from assetsWithContentURL1 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.thumbnailId and c.contentId != 755 ) , assetsWithContentURL3 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as highresURL, c.fileName as highresCntFileName from assetsWithContentURL2 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.highresCntId and c.contentId != 755 ) , assets as ( select a.*, case when right( a.contentURL, charindex( '.', reverse( a.contentURL )) - 1 ) in ( 'BMP','GIF','JPEG','JPG','PNG','TIF','TIFF','WBMP' ) then left( a.contentURL, len( a.contentURL ) - charindex( '.', reverse( a.contentURL ) )) + '_50' + right( a.contentURL, charindex( '.', reverse( a.contentURL )) ) else null end previewURL from assetsWithContentURL3 a ) , categories as ( select 'OPR' contentDomain, categoryId, categoryDescription from tbl_OPR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'CDR' contentDomain, categoryId, categoryDescription from tbl_CDR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'BPL' contentDomain, categoryId, categoryDescription from tbl_BPL_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'DOC' contentDomain, categoryId, categoryName as categoryDescription from tbl_DOC_Categories with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType ) , assetsWithCategory as ( select a.*, c.categoryDescription from assets a left outer join categories c on c.contentDomain = a.contentDomain and c.categoryId = a.categoryId ) ,industryCategories as ( SELECT categoryId, categoryDescription FROM tbl_PRD_RepositoryCategories with (nolock) WHERE containerType = 'SKU' AND ownerID = 0 AND ownerType = '*' AND left(categoryDescription, 3) <> 'GS1' ) , assetsWithIndustryCategory as ( select a.*, ic.categoryDescription as industryCategoryDescription from assetsWithCategory a left outer join industryCategories ic on ic.categoryId = a.categoryId ) , assetsWithPartner as ( select a.*, opr.companyName as partnerName, a.containerId as partnerId, a.containerType as partnerType, opr.territoryId from assetsWithIndustryCategory a inner join tbl_OPR_CLientOperators opr with (nolock) on opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = a.containerId where a.containerType = 'OPR' union all select a.*, cdr.cdr_dstName as partnerName, a.containerId as partnerId, a.containerType as partnerType, cdr.cdr_territoryId as territoryId from assetsWithIndustryCategory a inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = @ownerId and cdr.fsl_tablecode = @ownerType and cdr.cdr_recordId = a.containerId where a.containerType = 'CDR' union all select a.*, mfr.mfr_name as partnerName, mfr.mfr_id as partnerId, 'MFR' as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a inner join tbl_BRO_Principals bpl with (nolock) on bpl.ownerId = @ownerId and bpl.ownerType = @ownerType and bpl.principalId = a.containerId inner join tblManufacturers mfr with (nolock) on mfr.mfr_id = bpl.mfrId where a.containerType = 'BPL' union all select a.*,cast (null as varchar ) as partnerName, cast( null as integer ) as partnerId, cast( null as varchar ) as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a where coalesce( a.containerType, '???') not in ( 'BPL', 'OPR', 'CDR' ) ), assetsWithUser as ( select a.*, m.email updateUserEmail, m.firstName + ' ' + m.lastName as updateUserFullName, case when len(isnull(a.linked_prd,'')) > 0 or len(isnull(a.linked_sku,'')) > 0 then 'Y' else 'N' end as prdLink, case when len(isnull(a.linked_opr,'')) > 0 then 'Y' else 'N' end as oprLink, case when len(isnull(a.linked_cdr,'')) > 0 then 'Y' else 'N' end as cdrLink, case when len(isnull(a.linked_mfr,'')) > 0 then 'Y' else 'N' end as mfrLink from assetsWithPartner as a left outer join tbl_Fspro_members m with (nolock) on m.ownerId > 0 and m.fspro_userId = a.updateUserId ) , assetsWithUser_mappedData as ( select * from assetsWithUser ) , assetsWithUser_preData as ( select * from assetsWithUser_mappedData with (nolock) where ( ( 1 = 1 ) and ( ( ( [isPublished] = 'Y' ) or ( [isPublished] = 'F' ) ) ) ) ) , assetsWithUser_data as ( select * from assetsWithUser_preData ) , assetsWithUser_return as ( select * from assetsWithUser_data where 1 = 1 order by docTitle offset 0 rows fetch next 50 rows only ) , assetsWithUser_result as ( select ( select count(*) from assetsWithUser_data ) as totalCount, ( select * from assetsWithUser_return for json auto, include_null_values ) as data ) select * from assetsWithUser_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER -- 2026-04-16 12:56:43.773 -- 0 {ts '2026-04-16 17:29:59'} SQL declare @ownerId int = 207 ; declare @ownerType varchar(3) = 'MFR' ; declare @contentPrefix varchar(128) = 'http://dev.fsenablers.com/FSLContent/' ; declare @todayDate datetime = ?; with docCategories as ( SELECT cats.attrID as categoryId, cats.attrDescription AS categoryName FROM tbl_ORG_Attributes cats with (nolock) WHERE cats.OwnerID = @ownerId AND cats.FSL_TableCode = @ownerType AND cats.attrTypeID = 31 ), rawAssets as ( select null contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, cast( null as varchar ) docTitle, cast( null as int) as categoryId, cast( null as int ) contentId, cast( null as int) fileId, 0.00 fileSize, null fileExt, null docDescription, cast( null as datetime ) publishDate, cast( null as datetime ) unpublishDate, 'N' isPublished, cast( null as datetime ) uploadDate, cast( null as datetime ) updateDate, cast( null as int ) updateUserId, 0 restricted, -1 thumbnailId, -1 highresCntId, 99 as sortRank , 0 as freshness , 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr, cast( null as varchar ) as assetSelectionKey, 0 as selectedRecord , 'N' as internalOnly, '' as recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds union all select 'DOC' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, d.docTitle, d.repCategoryId as categoryId, d.contentId, d.docId as fileId, CAST((d.fileSize/1024) AS float) as fileSize , d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, case when d.publishDate is not null and d.publishDate > @todayDate then 'F' when d.publishDate is not null and d.unpublishDate is not null and @todayDate >= d.publishDate and @todayDate <= d.unpublishDate then 'Y' when d.publishDate is not null and d.unpublishDate is null and d.publishDate <= @todayDate then 'Y' when d.publishDate is null then 'N' else 'N' end isPublished, d.uploadDate, d.lastUpdatedDate updateDate, d.lastUpdatedBy updateUserId, 0 restricted, thumbnailId, -1 highresCntId, sortRank , coalesce(dateDiff(day, d.lastUpdatedDate, @todayDate),0) as freshness, case when d.privateYesNo = 1 then 'Y' else 'N' end privateYesNo, ou.firstname + ' ' + ou.lastname as uploadedByFullName, d.keyWords, d.unpublishToDo, d.internalNotes , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END), ',') AS linked_opr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END), ',') AS linked_prd , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END), ',') AS linked_sku , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END), ',') AS linked_cdr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END), ',') AS linked_mfr , cast(concat('DOC',':',d.docId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , d.internalOnly, d.recordSource , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 23 THEN li.child_attrID ELSE NULL END), ',') AS permissionGrpIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 7 THEN li.child_attrID ELSE NULL END), ',') AS linked_brokerIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 31 THEN li.child_attrID ELSE NULL END), ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = d.docId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 34 THEN li.child_attrID ELSE NULL END), ',') AS linked_UserMemberIds from tbl_DOC_Documents d with (nolock) LEFT OUTER JOIN tbl_FSPRO_Members ou with (nolock) ON d.AuthorUserId = ou.fspro_userId left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = d.docId and li.child_attrTypeID in (41,59,60,63,42,23,7,31,34) and li.mstr_attrTypeID = 30 left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('DOC',':',d.docId) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType GROUP BY d.docTitle, d.repCategoryId, d.contentId, d.docId,d.fileSize, d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, d.uploadDate, d.lastUpdatedDate, d.lastUpdatedBy,thumbnailId, sortRank , d.privateYesNo , ou.firstname,ou.lastname, d.keyWords, d.unpublishToDo, d.internalNotes,ast.assetSelectionKey, d.internalOnly, d.recordSource union all select 'IMG' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, i.imgTitle as docTitle, i.categoryId, i.contentId, i.imgId as fileId, CAST((i.fileSize/1024) AS float) as fileSize, i.fileExt, i.imgDescription as docDescription, i.publishDate, i.unpublishDate, case when i.publishDate is not null and i.publishDate > @todayDate then 'F' when i.publishDate is not null and i.unpublishDate is not null and @todayDate >= i.publishDate and @todayDate <= i.unpublishDate then 'Y' when i.publishDate is not null and i.unpublishDate is null and i.publishDate <= @todayDate then 'Y' when i.publishDate is null then 'N' else 'N' end isPublished, i.uploadDate, i.lastUpdatedDate updateDate, i.lastUpdatedBy updateUserId, 0 restricted, -1 thumbnailId, i.highresCntId, i.sortRank, coalesce(dateDiff(day, lastUpdatedDate, @todayDate),0) as freshness, case when i.privateYesNo = 1 then 'Y' else 'N' end privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , STRING_AGG(CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END, ',') AS linked_opr , STRING_AGG(CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END, ',') AS linked_prd , STRING_AGG(CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END, ',') AS linked_sku , STRING_AGG(CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END, ',') AS linked_cdr , STRING_AGG(CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END, ',') AS linked_mfr , cast(concat('IMG',':',i.imgId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , i.internalOnly, i.recordSource , STRING_AGG(CASE WHEN c.child_attrTypeID = 23 THEN c.child_attrID ELSE NULL END, ',') AS permissionGrpIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 7 THEN c.child_attrID ELSE NULL END, ',') AS linked_brokerIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 31 THEN c.child_attrID ELSE NULL END, ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = i.imgId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CASE WHEN c.child_attrTypeID = 34 THEN c.child_attrID ELSE NULL END, ',') AS linked_UserMemberIds from tbl_IMG_Images i with (nolock) left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = i.imgId and li.child_attrTypeID in (41,59,60,63,42) and li.mstr_attrTypeID = 33 left outer JOIN tbl_ORG_AttributeLinks c ON c.mstr_attrID = i.imgId and c.child_attrTypeID in (23,7,31,34) and c.mstr_attrTypeID = 30 left outer join tbl_DOC_Categories dc with (nolock) ON c.child_attrTypeID = 31 AND dc.categoryId = c.child_attrID AND dc.ownerId = @ownerId AND dc.fsl_tablecode = @ownerType left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('IMG',':',i.imgId) where i.ownerId = @ownerId and i.fsl_tablecode = @ownerType GROUP BY i.imgTitle, i.CategoryID, i.contentId, i.imgId, i.fileSize, i.fileExt, i.imgDescription, i.publishDate, i.unpublishDate, i.uploadDate, i.lastUpdatedDate, i.lastUpdatedBy, i.highresCntId, i.sortRank, i.privateYesNo,ast.assetSelectionKey, i.internalOnly, i.recordSource ), filteredRawAssets as ( select ra.* from rawAssets ra where (1=1 or ra.linked_brokerIds in (?) ) ) , assetsWithContentURL1 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL, c.fileName as docFileName, lub.firstname + ' ' + lub.lastname as lastUpdatedByFullName from filteredRawAssets ra LEFT OUTER JOIN tbl_FSPRO_Members lub with (nolock) ON ra.updateUserId = lub.fspro_userId inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) , assetsWithContentURL2 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as thumbnailURL, c.fileName as thnFileName from assetsWithContentURL1 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.thumbnailId and c.contentId != 755 ) , assetsWithContentURL3 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as highresURL, c.fileName as highresCntFileName from assetsWithContentURL2 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.highresCntId and c.contentId != 755 ) , assets as ( select a.*, case when right( a.contentURL, charindex( '.', reverse( a.contentURL )) - 1 ) in ( 'BMP','GIF','JPEG','JPG','PNG','TIF','TIFF','WBMP' ) then left( a.contentURL, len( a.contentURL ) - charindex( '.', reverse( a.contentURL ) )) + '_50' + right( a.contentURL, charindex( '.', reverse( a.contentURL )) ) else null end previewURL from assetsWithContentURL3 a ) , categories as ( select 'OPR' contentDomain, categoryId, categoryDescription from tbl_OPR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'CDR' contentDomain, categoryId, categoryDescription from tbl_CDR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'BPL' contentDomain, categoryId, categoryDescription from tbl_BPL_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'DOC' contentDomain, categoryId, categoryName as categoryDescription from tbl_DOC_Categories with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType ) , assetsWithCategory as ( select a.*, c.categoryDescription from assets a left outer join categories c on c.contentDomain = a.contentDomain and c.categoryId = a.categoryId ) ,industryCategories as ( SELECT categoryId, categoryDescription FROM tbl_PRD_RepositoryCategories with (nolock) WHERE containerType = 'SKU' AND ownerID = 0 AND ownerType = '*' AND left(categoryDescription, 3) <> 'GS1' ) , assetsWithIndustryCategory as ( select a.*, ic.categoryDescription as industryCategoryDescription from assetsWithCategory a left outer join industryCategories ic on ic.categoryId = a.categoryId ) , assetsWithPartner as ( select a.*, opr.companyName as partnerName, a.containerId as partnerId, a.containerType as partnerType, opr.territoryId from assetsWithIndustryCategory a inner join tbl_OPR_CLientOperators opr with (nolock) on opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = a.containerId where a.containerType = 'OPR' union all select a.*, cdr.cdr_dstName as partnerName, a.containerId as partnerId, a.containerType as partnerType, cdr.cdr_territoryId as territoryId from assetsWithIndustryCategory a inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = @ownerId and cdr.fsl_tablecode = @ownerType and cdr.cdr_recordId = a.containerId where a.containerType = 'CDR' union all select a.*, mfr.mfr_name as partnerName, mfr.mfr_id as partnerId, 'MFR' as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a inner join tbl_BRO_Principals bpl with (nolock) on bpl.ownerId = @ownerId and bpl.ownerType = @ownerType and bpl.principalId = a.containerId inner join tblManufacturers mfr with (nolock) on mfr.mfr_id = bpl.mfrId where a.containerType = 'BPL' union all select a.*,cast (null as varchar ) as partnerName, cast( null as integer ) as partnerId, cast( null as varchar ) as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a where coalesce( a.containerType, '???') not in ( 'BPL', 'OPR', 'CDR' ) ), assetsWithUser as ( select a.*, m.email updateUserEmail, m.firstName + ' ' + m.lastName as updateUserFullName, case when len(isnull(a.linked_prd,'')) > 0 or len(isnull(a.linked_sku,'')) > 0 then 'Y' else 'N' end as prdLink, case when len(isnull(a.linked_opr,'')) > 0 then 'Y' else 'N' end as oprLink, case when len(isnull(a.linked_cdr,'')) > 0 then 'Y' else 'N' end as cdrLink, case when len(isnull(a.linked_mfr,'')) > 0 then 'Y' else 'N' end as mfrLink from assetsWithPartner as a left outer join tbl_Fspro_members m with (nolock) on m.ownerId > 0 and m.fspro_userId = a.updateUserId ) , assetsWithUser_mappedData as ( select * from assetsWithUser ) , assetsWithUser_preData as ( select * from assetsWithUser_mappedData with (nolock) where ( ( 1 = 1 ) and ( ( ( [isPublished] = 'Y' ) or ( [isPublished] = 'F' ) ) ) ) ) , assetsWithUser_data as ( select * from assetsWithUser_preData ) , assetsWithUser_return as ( select * from assetsWithUser_data where 1 = 1 order by docTitle offset 0 rows fetch next 50 rows only ) , assetsWithUser_result as ( select ( select count(*) from assetsWithUser_data ) as totalCount, ( select * from assetsWithUser_return for json auto, include_null_values ) as data ) select * from assetsWithUser_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER -- 2026-04-16 17:29:59.301 -- 0 {ts '2026-04-16 17:38:12'} SQL declare @ownerId int = 207 ; declare @ownerType varchar(3) = 'MFR' ; declare @contentPrefix varchar(128) = 'http://dev.fsenablers.com/FSLContent/' ; declare @todayDate datetime = ?; with docCategories as ( SELECT cats.attrID as categoryId, cats.attrDescription AS categoryName FROM tbl_ORG_Attributes cats with (nolock) WHERE cats.OwnerID = @ownerId AND cats.FSL_TableCode = @ownerType AND cats.attrTypeID = 31 ), rawAssets as ( select null contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, cast( null as varchar ) docTitle, cast( null as int) as categoryId, cast( null as int ) contentId, cast( null as int) fileId, 0.00 fileSize, null fileExt, null docDescription, cast( null as datetime ) publishDate, cast( null as datetime ) unpublishDate, 'N' isPublished, cast( null as datetime ) uploadDate, cast( null as datetime ) updateDate, cast( null as int ) updateUserId, 0 restricted, -1 thumbnailId, -1 highresCntId, 99 as sortRank , 0 as freshness , 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr, cast( null as varchar ) as assetSelectionKey, 0 as selectedRecord , 'N' as internalOnly, '' as recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds union all select 'DOC' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, d.docTitle, d.repCategoryId as categoryId, d.contentId, d.docId as fileId, CAST((d.fileSize/1024) AS float) as fileSize , d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, case when d.publishDate is not null and d.publishDate > @todayDate then 'F' when d.publishDate is not null and d.unpublishDate is not null and @todayDate >= d.publishDate and @todayDate <= d.unpublishDate then 'Y' when d.publishDate is not null and d.unpublishDate is null and d.publishDate <= @todayDate then 'Y' when d.publishDate is null then 'N' else 'N' end isPublished, d.uploadDate, d.lastUpdatedDate updateDate, d.lastUpdatedBy updateUserId, 0 restricted, thumbnailId, -1 highresCntId, sortRank , coalesce(dateDiff(day, d.lastUpdatedDate, @todayDate),0) as freshness, case when d.privateYesNo = 1 then 'Y' else 'N' end privateYesNo, ou.firstname + ' ' + ou.lastname as uploadedByFullName, d.keyWords, d.unpublishToDo, d.internalNotes , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END), ',') AS linked_opr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END), ',') AS linked_prd , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END), ',') AS linked_sku , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END), ',') AS linked_cdr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END), ',') AS linked_mfr , cast(concat('DOC',':',d.docId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , d.internalOnly, d.recordSource , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 23 THEN li.child_attrID ELSE NULL END), ',') AS permissionGrpIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 7 THEN li.child_attrID ELSE NULL END), ',') AS linked_brokerIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 31 THEN li.child_attrID ELSE NULL END), ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = d.docId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 34 THEN li.child_attrID ELSE NULL END), ',') AS linked_UserMemberIds from tbl_DOC_Documents d with (nolock) LEFT OUTER JOIN tbl_FSPRO_Members ou with (nolock) ON d.AuthorUserId = ou.fspro_userId left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = d.docId and li.child_attrTypeID in (41,59,60,63,42,23,7,31,34) and li.mstr_attrTypeID = 30 left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('DOC',':',d.docId) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType GROUP BY d.docTitle, d.repCategoryId, d.contentId, d.docId,d.fileSize, d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, d.uploadDate, d.lastUpdatedDate, d.lastUpdatedBy,thumbnailId, sortRank , d.privateYesNo , ou.firstname,ou.lastname, d.keyWords, d.unpublishToDo, d.internalNotes,ast.assetSelectionKey, d.internalOnly, d.recordSource union all select 'IMG' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, i.imgTitle as docTitle, i.categoryId, i.contentId, i.imgId as fileId, CAST((i.fileSize/1024) AS float) as fileSize, i.fileExt, i.imgDescription as docDescription, i.publishDate, i.unpublishDate, case when i.publishDate is not null and i.publishDate > @todayDate then 'F' when i.publishDate is not null and i.unpublishDate is not null and @todayDate >= i.publishDate and @todayDate <= i.unpublishDate then 'Y' when i.publishDate is not null and i.unpublishDate is null and i.publishDate <= @todayDate then 'Y' when i.publishDate is null then 'N' else 'N' end isPublished, i.uploadDate, i.lastUpdatedDate updateDate, i.lastUpdatedBy updateUserId, 0 restricted, -1 thumbnailId, i.highresCntId, i.sortRank, coalesce(dateDiff(day, lastUpdatedDate, @todayDate),0) as freshness, case when i.privateYesNo = 1 then 'Y' else 'N' end privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , STRING_AGG(CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END, ',') AS linked_opr , STRING_AGG(CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END, ',') AS linked_prd , STRING_AGG(CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END, ',') AS linked_sku , STRING_AGG(CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END, ',') AS linked_cdr , STRING_AGG(CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END, ',') AS linked_mfr , cast(concat('IMG',':',i.imgId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , i.internalOnly, i.recordSource , STRING_AGG(CASE WHEN c.child_attrTypeID = 23 THEN c.child_attrID ELSE NULL END, ',') AS permissionGrpIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 7 THEN c.child_attrID ELSE NULL END, ',') AS linked_brokerIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 31 THEN c.child_attrID ELSE NULL END, ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = i.imgId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CASE WHEN c.child_attrTypeID = 34 THEN c.child_attrID ELSE NULL END, ',') AS linked_UserMemberIds from tbl_IMG_Images i with (nolock) left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = i.imgId and li.child_attrTypeID in (41,59,60,63,42) and li.mstr_attrTypeID = 33 left outer JOIN tbl_ORG_AttributeLinks c ON c.mstr_attrID = i.imgId and c.child_attrTypeID in (23,7,31,34) and c.mstr_attrTypeID = 30 left outer join tbl_DOC_Categories dc with (nolock) ON c.child_attrTypeID = 31 AND dc.categoryId = c.child_attrID AND dc.ownerId = @ownerId AND dc.fsl_tablecode = @ownerType left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('IMG',':',i.imgId) where i.ownerId = @ownerId and i.fsl_tablecode = @ownerType GROUP BY i.imgTitle, i.CategoryID, i.contentId, i.imgId, i.fileSize, i.fileExt, i.imgDescription, i.publishDate, i.unpublishDate, i.uploadDate, i.lastUpdatedDate, i.lastUpdatedBy, i.highresCntId, i.sortRank, i.privateYesNo,ast.assetSelectionKey, i.internalOnly, i.recordSource ), filteredRawAssets as ( select ra.* from rawAssets ra where (1=1 or ra.linked_brokerIds in (?) ) ) , assetsWithContentURL1 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL, c.fileName as docFileName, lub.firstname + ' ' + lub.lastname as lastUpdatedByFullName from filteredRawAssets ra LEFT OUTER JOIN tbl_FSPRO_Members lub with (nolock) ON ra.updateUserId = lub.fspro_userId inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) , assetsWithContentURL2 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as thumbnailURL, c.fileName as thnFileName from assetsWithContentURL1 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.thumbnailId and c.contentId != 755 ) , assetsWithContentURL3 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as highresURL, c.fileName as highresCntFileName from assetsWithContentURL2 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.highresCntId and c.contentId != 755 ) , assets as ( select a.*, case when right( a.contentURL, charindex( '.', reverse( a.contentURL )) - 1 ) in ( 'BMP','GIF','JPEG','JPG','PNG','TIF','TIFF','WBMP' ) then left( a.contentURL, len( a.contentURL ) - charindex( '.', reverse( a.contentURL ) )) + '_50' + right( a.contentURL, charindex( '.', reverse( a.contentURL )) ) else null end previewURL from assetsWithContentURL3 a ) , categories as ( select 'OPR' contentDomain, categoryId, categoryDescription from tbl_OPR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'CDR' contentDomain, categoryId, categoryDescription from tbl_CDR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'BPL' contentDomain, categoryId, categoryDescription from tbl_BPL_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'DOC' contentDomain, categoryId, categoryName as categoryDescription from tbl_DOC_Categories with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType ) , assetsWithCategory as ( select a.*, c.categoryDescription from assets a left outer join categories c on c.contentDomain = a.contentDomain and c.categoryId = a.categoryId ) ,industryCategories as ( SELECT categoryId, categoryDescription FROM tbl_PRD_RepositoryCategories with (nolock) WHERE containerType = 'SKU' AND ownerID = 0 AND ownerType = '*' AND left(categoryDescription, 3) <> 'GS1' ) , assetsWithIndustryCategory as ( select a.*, ic.categoryDescription as industryCategoryDescription from assetsWithCategory a left outer join industryCategories ic on ic.categoryId = a.categoryId ) , assetsWithPartner as ( select a.*, opr.companyName as partnerName, a.containerId as partnerId, a.containerType as partnerType, opr.territoryId from assetsWithIndustryCategory a inner join tbl_OPR_CLientOperators opr with (nolock) on opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = a.containerId where a.containerType = 'OPR' union all select a.*, cdr.cdr_dstName as partnerName, a.containerId as partnerId, a.containerType as partnerType, cdr.cdr_territoryId as territoryId from assetsWithIndustryCategory a inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = @ownerId and cdr.fsl_tablecode = @ownerType and cdr.cdr_recordId = a.containerId where a.containerType = 'CDR' union all select a.*, mfr.mfr_name as partnerName, mfr.mfr_id as partnerId, 'MFR' as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a inner join tbl_BRO_Principals bpl with (nolock) on bpl.ownerId = @ownerId and bpl.ownerType = @ownerType and bpl.principalId = a.containerId inner join tblManufacturers mfr with (nolock) on mfr.mfr_id = bpl.mfrId where a.containerType = 'BPL' union all select a.*,cast (null as varchar ) as partnerName, cast( null as integer ) as partnerId, cast( null as varchar ) as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a where coalesce( a.containerType, '???') not in ( 'BPL', 'OPR', 'CDR' ) ), assetsWithUser as ( select a.*, m.email updateUserEmail, m.firstName + ' ' + m.lastName as updateUserFullName, case when len(isnull(a.linked_prd,'')) > 0 or len(isnull(a.linked_sku,'')) > 0 then 'Y' else 'N' end as prdLink, case when len(isnull(a.linked_opr,'')) > 0 then 'Y' else 'N' end as oprLink, case when len(isnull(a.linked_cdr,'')) > 0 then 'Y' else 'N' end as cdrLink, case when len(isnull(a.linked_mfr,'')) > 0 then 'Y' else 'N' end as mfrLink from assetsWithPartner as a left outer join tbl_Fspro_members m with (nolock) on m.ownerId > 0 and m.fspro_userId = a.updateUserId ) , assetsWithUser_mappedData as ( select * from assetsWithUser ) , assetsWithUser_preData as ( select * from assetsWithUser_mappedData with (nolock) where ( ( 1 = 1 ) and ( ( ( [isPublished] = 'Y' ) or ( [isPublished] = 'F' ) ) ) ) ) , assetsWithUser_data as ( select * from assetsWithUser_preData ) , assetsWithUser_return as ( select * from assetsWithUser_data where 1 = 1 order by docTitle offset 0 rows fetch next 50 rows only ) , assetsWithUser_result as ( select ( select count(*) from assetsWithUser_data ) as totalCount, ( select * from assetsWithUser_return for json auto, include_null_values ) as data ) select * from assetsWithUser_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER -- 2026-04-16 17:38:12.327 -- 0 {ts '2026-04-16 17:39:28'} SQL declare @ownerId int = 207 ; declare @ownerType varchar(3) = 'MFR' ; declare @contentPrefix varchar(128) = 'http://dev.fsenablers.com/FSLContent/' ; declare @todayDate datetime = ?; with docCategories as ( SELECT cats.attrID as categoryId, cats.attrDescription AS categoryName FROM tbl_ORG_Attributes cats with (nolock) WHERE cats.OwnerID = @ownerId AND cats.FSL_TableCode = @ownerType AND cats.attrTypeID = 31 ), rawAssets as ( select null contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, cast( null as varchar ) docTitle, cast( null as int) as categoryId, cast( null as int ) contentId, cast( null as int) fileId, 0.00 fileSize, null fileExt, null docDescription, cast( null as datetime ) publishDate, cast( null as datetime ) unpublishDate, 'N' isPublished, cast( null as datetime ) uploadDate, cast( null as datetime ) updateDate, cast( null as int ) updateUserId, 0 restricted, -1 thumbnailId, -1 highresCntId, 99 as sortRank , 0 as freshness , 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr, cast( null as varchar ) as assetSelectionKey, 0 as selectedRecord , 'N' as internalOnly, '' as recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds union all select 'DOC' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, d.docTitle, d.repCategoryId as categoryId, d.contentId, d.docId as fileId, CAST((d.fileSize/1024) AS float) as fileSize , d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, case when d.publishDate is not null and d.publishDate > @todayDate then 'F' when d.publishDate is not null and d.unpublishDate is not null and @todayDate >= d.publishDate and @todayDate <= d.unpublishDate then 'Y' when d.publishDate is not null and d.unpublishDate is null and d.publishDate <= @todayDate then 'Y' when d.publishDate is null then 'N' else 'N' end isPublished, d.uploadDate, d.lastUpdatedDate updateDate, d.lastUpdatedBy updateUserId, 0 restricted, thumbnailId, -1 highresCntId, sortRank , coalesce(dateDiff(day, d.lastUpdatedDate, @todayDate),0) as freshness, case when d.privateYesNo = 1 then 'Y' else 'N' end privateYesNo, ou.firstname + ' ' + ou.lastname as uploadedByFullName, d.keyWords, d.unpublishToDo, d.internalNotes , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END), ',') AS linked_opr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END), ',') AS linked_prd , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END), ',') AS linked_sku , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END), ',') AS linked_cdr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END), ',') AS linked_mfr , cast(concat('DOC',':',d.docId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , d.internalOnly, d.recordSource , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 23 THEN li.child_attrID ELSE NULL END), ',') AS permissionGrpIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 7 THEN li.child_attrID ELSE NULL END), ',') AS linked_brokerIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 31 THEN li.child_attrID ELSE NULL END), ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = d.docId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 34 THEN li.child_attrID ELSE NULL END), ',') AS linked_UserMemberIds from tbl_DOC_Documents d with (nolock) LEFT OUTER JOIN tbl_FSPRO_Members ou with (nolock) ON d.AuthorUserId = ou.fspro_userId left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = d.docId and li.child_attrTypeID in (41,59,60,63,42,23,7,31,34) and li.mstr_attrTypeID = 30 left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('DOC',':',d.docId) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType GROUP BY d.docTitle, d.repCategoryId, d.contentId, d.docId,d.fileSize, d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, d.uploadDate, d.lastUpdatedDate, d.lastUpdatedBy,thumbnailId, sortRank , d.privateYesNo , ou.firstname,ou.lastname, d.keyWords, d.unpublishToDo, d.internalNotes,ast.assetSelectionKey, d.internalOnly, d.recordSource union all select 'IMG' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, i.imgTitle as docTitle, i.categoryId, i.contentId, i.imgId as fileId, CAST((i.fileSize/1024) AS float) as fileSize, i.fileExt, i.imgDescription as docDescription, i.publishDate, i.unpublishDate, case when i.publishDate is not null and i.publishDate > @todayDate then 'F' when i.publishDate is not null and i.unpublishDate is not null and @todayDate >= i.publishDate and @todayDate <= i.unpublishDate then 'Y' when i.publishDate is not null and i.unpublishDate is null and i.publishDate <= @todayDate then 'Y' when i.publishDate is null then 'N' else 'N' end isPublished, i.uploadDate, i.lastUpdatedDate updateDate, i.lastUpdatedBy updateUserId, 0 restricted, -1 thumbnailId, i.highresCntId, i.sortRank, coalesce(dateDiff(day, lastUpdatedDate, @todayDate),0) as freshness, case when i.privateYesNo = 1 then 'Y' else 'N' end privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , STRING_AGG(CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END, ',') AS linked_opr , STRING_AGG(CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END, ',') AS linked_prd , STRING_AGG(CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END, ',') AS linked_sku , STRING_AGG(CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END, ',') AS linked_cdr , STRING_AGG(CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END, ',') AS linked_mfr , cast(concat('IMG',':',i.imgId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , i.internalOnly, i.recordSource , STRING_AGG(CASE WHEN c.child_attrTypeID = 23 THEN c.child_attrID ELSE NULL END, ',') AS permissionGrpIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 7 THEN c.child_attrID ELSE NULL END, ',') AS linked_brokerIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 31 THEN c.child_attrID ELSE NULL END, ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = i.imgId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CASE WHEN c.child_attrTypeID = 34 THEN c.child_attrID ELSE NULL END, ',') AS linked_UserMemberIds from tbl_IMG_Images i with (nolock) left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = i.imgId and li.child_attrTypeID in (41,59,60,63,42) and li.mstr_attrTypeID = 33 left outer JOIN tbl_ORG_AttributeLinks c ON c.mstr_attrID = i.imgId and c.child_attrTypeID in (23,7,31,34) and c.mstr_attrTypeID = 30 left outer join tbl_DOC_Categories dc with (nolock) ON c.child_attrTypeID = 31 AND dc.categoryId = c.child_attrID AND dc.ownerId = @ownerId AND dc.fsl_tablecode = @ownerType left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('IMG',':',i.imgId) where i.ownerId = @ownerId and i.fsl_tablecode = @ownerType GROUP BY i.imgTitle, i.CategoryID, i.contentId, i.imgId, i.fileSize, i.fileExt, i.imgDescription, i.publishDate, i.unpublishDate, i.uploadDate, i.lastUpdatedDate, i.lastUpdatedBy, i.highresCntId, i.sortRank, i.privateYesNo,ast.assetSelectionKey, i.internalOnly, i.recordSource ), filteredRawAssets as ( select ra.* from rawAssets ra where (1=1 or ra.linked_brokerIds in (?) ) ) , assetsWithContentURL1 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL, c.fileName as docFileName, lub.firstname + ' ' + lub.lastname as lastUpdatedByFullName from filteredRawAssets ra LEFT OUTER JOIN tbl_FSPRO_Members lub with (nolock) ON ra.updateUserId = lub.fspro_userId inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) , assetsWithContentURL2 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as thumbnailURL, c.fileName as thnFileName from assetsWithContentURL1 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.thumbnailId and c.contentId != 755 ) , assetsWithContentURL3 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as highresURL, c.fileName as highresCntFileName from assetsWithContentURL2 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.highresCntId and c.contentId != 755 ) , assets as ( select a.*, case when right( a.contentURL, charindex( '.', reverse( a.contentURL )) - 1 ) in ( 'BMP','GIF','JPEG','JPG','PNG','TIF','TIFF','WBMP' ) then left( a.contentURL, len( a.contentURL ) - charindex( '.', reverse( a.contentURL ) )) + '_50' + right( a.contentURL, charindex( '.', reverse( a.contentURL )) ) else null end previewURL from assetsWithContentURL3 a ) , categories as ( select 'OPR' contentDomain, categoryId, categoryDescription from tbl_OPR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'CDR' contentDomain, categoryId, categoryDescription from tbl_CDR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'BPL' contentDomain, categoryId, categoryDescription from tbl_BPL_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'DOC' contentDomain, categoryId, categoryName as categoryDescription from tbl_DOC_Categories with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType ) , assetsWithCategory as ( select a.*, c.categoryDescription from assets a left outer join categories c on c.contentDomain = a.contentDomain and c.categoryId = a.categoryId ) ,industryCategories as ( SELECT categoryId, categoryDescription FROM tbl_PRD_RepositoryCategories with (nolock) WHERE containerType = 'SKU' AND ownerID = 0 AND ownerType = '*' AND left(categoryDescription, 3) <> 'GS1' ) , assetsWithIndustryCategory as ( select a.*, ic.categoryDescription as industryCategoryDescription from assetsWithCategory a left outer join industryCategories ic on ic.categoryId = a.categoryId ) , assetsWithPartner as ( select a.*, opr.companyName as partnerName, a.containerId as partnerId, a.containerType as partnerType, opr.territoryId from assetsWithIndustryCategory a inner join tbl_OPR_CLientOperators opr with (nolock) on opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = a.containerId where a.containerType = 'OPR' union all select a.*, cdr.cdr_dstName as partnerName, a.containerId as partnerId, a.containerType as partnerType, cdr.cdr_territoryId as territoryId from assetsWithIndustryCategory a inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = @ownerId and cdr.fsl_tablecode = @ownerType and cdr.cdr_recordId = a.containerId where a.containerType = 'CDR' union all select a.*, mfr.mfr_name as partnerName, mfr.mfr_id as partnerId, 'MFR' as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a inner join tbl_BRO_Principals bpl with (nolock) on bpl.ownerId = @ownerId and bpl.ownerType = @ownerType and bpl.principalId = a.containerId inner join tblManufacturers mfr with (nolock) on mfr.mfr_id = bpl.mfrId where a.containerType = 'BPL' union all select a.*,cast (null as varchar ) as partnerName, cast( null as integer ) as partnerId, cast( null as varchar ) as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a where coalesce( a.containerType, '???') not in ( 'BPL', 'OPR', 'CDR' ) ), assetsWithUser as ( select a.*, m.email updateUserEmail, m.firstName + ' ' + m.lastName as updateUserFullName, case when len(isnull(a.linked_prd,'')) > 0 or len(isnull(a.linked_sku,'')) > 0 then 'Y' else 'N' end as prdLink, case when len(isnull(a.linked_opr,'')) > 0 then 'Y' else 'N' end as oprLink, case when len(isnull(a.linked_cdr,'')) > 0 then 'Y' else 'N' end as cdrLink, case when len(isnull(a.linked_mfr,'')) > 0 then 'Y' else 'N' end as mfrLink from assetsWithPartner as a left outer join tbl_Fspro_members m with (nolock) on m.ownerId > 0 and m.fspro_userId = a.updateUserId ) , assetsWithUser_mappedData as ( select * from assetsWithUser ) , assetsWithUser_preData as ( select * from assetsWithUser_mappedData with (nolock) where ( ( 1 = 1 ) and ( ( ( [isPublished] = 'Y' ) or ( [isPublished] = 'F' ) ) ) ) ) , assetsWithUser_data as ( select * from assetsWithUser_preData ) , assetsWithUser_return as ( select * from assetsWithUser_data where 1 = 1 order by docTitle offset 0 rows fetch next 50 rows only ) , assetsWithUser_result as ( select ( select count(*) from assetsWithUser_data ) as totalCount, ( select * from assetsWithUser_return for json auto, include_null_values ) as data ) select * from assetsWithUser_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER -- 2026-04-16 17:39:28.454 -- 0 {ts '2026-04-16 17:39:34'} SQL declare @ownerId int = 207 ; declare @ownerType varchar(3) = 'MFR' ; declare @contentPrefix varchar(128) = 'http://dev.fsenablers.com/FSLContent/' ; declare @todayDate datetime = ?; with docCategories as ( SELECT cats.attrID as categoryId, cats.attrDescription AS categoryName FROM tbl_ORG_Attributes cats with (nolock) WHERE cats.OwnerID = @ownerId AND cats.FSL_TableCode = @ownerType AND cats.attrTypeID = 31 ), rawAssets as ( select null contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, cast( null as varchar ) docTitle, cast( null as int) as categoryId, cast( null as int ) contentId, cast( null as int) fileId, 0.00 fileSize, null fileExt, null docDescription, cast( null as datetime ) publishDate, cast( null as datetime ) unpublishDate, 'N' isPublished, cast( null as datetime ) uploadDate, cast( null as datetime ) updateDate, cast( null as int ) updateUserId, 0 restricted, -1 thumbnailId, -1 highresCntId, 99 as sortRank , 0 as freshness , 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr, cast( null as varchar ) as assetSelectionKey, 0 as selectedRecord , 'N' as internalOnly, '' as recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds union all select 'DOC' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, d.docTitle, d.repCategoryId as categoryId, d.contentId, d.docId as fileId, CAST((d.fileSize/1024) AS float) as fileSize , d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, case when d.publishDate is not null and d.publishDate > @todayDate then 'F' when d.publishDate is not null and d.unpublishDate is not null and @todayDate >= d.publishDate and @todayDate <= d.unpublishDate then 'Y' when d.publishDate is not null and d.unpublishDate is null and d.publishDate <= @todayDate then 'Y' when d.publishDate is null then 'N' else 'N' end isPublished, d.uploadDate, d.lastUpdatedDate updateDate, d.lastUpdatedBy updateUserId, 0 restricted, thumbnailId, -1 highresCntId, sortRank , coalesce(dateDiff(day, d.lastUpdatedDate, @todayDate),0) as freshness, case when d.privateYesNo = 1 then 'Y' else 'N' end privateYesNo, ou.firstname + ' ' + ou.lastname as uploadedByFullName, d.keyWords, d.unpublishToDo, d.internalNotes , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END), ',') AS linked_opr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END), ',') AS linked_prd , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END), ',') AS linked_sku , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END), ',') AS linked_cdr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END), ',') AS linked_mfr , cast(concat('DOC',':',d.docId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , d.internalOnly, d.recordSource , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 23 THEN li.child_attrID ELSE NULL END), ',') AS permissionGrpIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 7 THEN li.child_attrID ELSE NULL END), ',') AS linked_brokerIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 31 THEN li.child_attrID ELSE NULL END), ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = d.docId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 34 THEN li.child_attrID ELSE NULL END), ',') AS linked_UserMemberIds from tbl_DOC_Documents d with (nolock) LEFT OUTER JOIN tbl_FSPRO_Members ou with (nolock) ON d.AuthorUserId = ou.fspro_userId left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = d.docId and li.child_attrTypeID in (41,59,60,63,42,23,7,31,34) and li.mstr_attrTypeID = 30 left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('DOC',':',d.docId) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType GROUP BY d.docTitle, d.repCategoryId, d.contentId, d.docId,d.fileSize, d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, d.uploadDate, d.lastUpdatedDate, d.lastUpdatedBy,thumbnailId, sortRank , d.privateYesNo , ou.firstname,ou.lastname, d.keyWords, d.unpublishToDo, d.internalNotes,ast.assetSelectionKey, d.internalOnly, d.recordSource union all select 'IMG' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, i.imgTitle as docTitle, i.categoryId, i.contentId, i.imgId as fileId, CAST((i.fileSize/1024) AS float) as fileSize, i.fileExt, i.imgDescription as docDescription, i.publishDate, i.unpublishDate, case when i.publishDate is not null and i.publishDate > @todayDate then 'F' when i.publishDate is not null and i.unpublishDate is not null and @todayDate >= i.publishDate and @todayDate <= i.unpublishDate then 'Y' when i.publishDate is not null and i.unpublishDate is null and i.publishDate <= @todayDate then 'Y' when i.publishDate is null then 'N' else 'N' end isPublished, i.uploadDate, i.lastUpdatedDate updateDate, i.lastUpdatedBy updateUserId, 0 restricted, -1 thumbnailId, i.highresCntId, i.sortRank, coalesce(dateDiff(day, lastUpdatedDate, @todayDate),0) as freshness, case when i.privateYesNo = 1 then 'Y' else 'N' end privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , STRING_AGG(CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END, ',') AS linked_opr , STRING_AGG(CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END, ',') AS linked_prd , STRING_AGG(CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END, ',') AS linked_sku , STRING_AGG(CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END, ',') AS linked_cdr , STRING_AGG(CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END, ',') AS linked_mfr , cast(concat('IMG',':',i.imgId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , i.internalOnly, i.recordSource , STRING_AGG(CASE WHEN c.child_attrTypeID = 23 THEN c.child_attrID ELSE NULL END, ',') AS permissionGrpIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 7 THEN c.child_attrID ELSE NULL END, ',') AS linked_brokerIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 31 THEN c.child_attrID ELSE NULL END, ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = i.imgId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CASE WHEN c.child_attrTypeID = 34 THEN c.child_attrID ELSE NULL END, ',') AS linked_UserMemberIds from tbl_IMG_Images i with (nolock) left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = i.imgId and li.child_attrTypeID in (41,59,60,63,42) and li.mstr_attrTypeID = 33 left outer JOIN tbl_ORG_AttributeLinks c ON c.mstr_attrID = i.imgId and c.child_attrTypeID in (23,7,31,34) and c.mstr_attrTypeID = 30 left outer join tbl_DOC_Categories dc with (nolock) ON c.child_attrTypeID = 31 AND dc.categoryId = c.child_attrID AND dc.ownerId = @ownerId AND dc.fsl_tablecode = @ownerType left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('IMG',':',i.imgId) where i.ownerId = @ownerId and i.fsl_tablecode = @ownerType GROUP BY i.imgTitle, i.CategoryID, i.contentId, i.imgId, i.fileSize, i.fileExt, i.imgDescription, i.publishDate, i.unpublishDate, i.uploadDate, i.lastUpdatedDate, i.lastUpdatedBy, i.highresCntId, i.sortRank, i.privateYesNo,ast.assetSelectionKey, i.internalOnly, i.recordSource ), filteredRawAssets as ( select ra.* from rawAssets ra where (1=1 or ra.linked_brokerIds in (?) ) ) , assetsWithContentURL1 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL, c.fileName as docFileName, lub.firstname + ' ' + lub.lastname as lastUpdatedByFullName from filteredRawAssets ra LEFT OUTER JOIN tbl_FSPRO_Members lub with (nolock) ON ra.updateUserId = lub.fspro_userId inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) , assetsWithContentURL2 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as thumbnailURL, c.fileName as thnFileName from assetsWithContentURL1 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.thumbnailId and c.contentId != 755 ) , assetsWithContentURL3 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as highresURL, c.fileName as highresCntFileName from assetsWithContentURL2 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.highresCntId and c.contentId != 755 ) , assets as ( select a.*, case when right( a.contentURL, charindex( '.', reverse( a.contentURL )) - 1 ) in ( 'BMP','GIF','JPEG','JPG','PNG','TIF','TIFF','WBMP' ) then left( a.contentURL, len( a.contentURL ) - charindex( '.', reverse( a.contentURL ) )) + '_50' + right( a.contentURL, charindex( '.', reverse( a.contentURL )) ) else null end previewURL from assetsWithContentURL3 a ) , categories as ( select 'OPR' contentDomain, categoryId, categoryDescription from tbl_OPR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'CDR' contentDomain, categoryId, categoryDescription from tbl_CDR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'BPL' contentDomain, categoryId, categoryDescription from tbl_BPL_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'DOC' contentDomain, categoryId, categoryName as categoryDescription from tbl_DOC_Categories with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType ) , assetsWithCategory as ( select a.*, c.categoryDescription from assets a left outer join categories c on c.contentDomain = a.contentDomain and c.categoryId = a.categoryId ) ,industryCategories as ( SELECT categoryId, categoryDescription FROM tbl_PRD_RepositoryCategories with (nolock) WHERE containerType = 'SKU' AND ownerID = 0 AND ownerType = '*' AND left(categoryDescription, 3) <> 'GS1' ) , assetsWithIndustryCategory as ( select a.*, ic.categoryDescription as industryCategoryDescription from assetsWithCategory a left outer join industryCategories ic on ic.categoryId = a.categoryId ) , assetsWithPartner as ( select a.*, opr.companyName as partnerName, a.containerId as partnerId, a.containerType as partnerType, opr.territoryId from assetsWithIndustryCategory a inner join tbl_OPR_CLientOperators opr with (nolock) on opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = a.containerId where a.containerType = 'OPR' union all select a.*, cdr.cdr_dstName as partnerName, a.containerId as partnerId, a.containerType as partnerType, cdr.cdr_territoryId as territoryId from assetsWithIndustryCategory a inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = @ownerId and cdr.fsl_tablecode = @ownerType and cdr.cdr_recordId = a.containerId where a.containerType = 'CDR' union all select a.*, mfr.mfr_name as partnerName, mfr.mfr_id as partnerId, 'MFR' as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a inner join tbl_BRO_Principals bpl with (nolock) on bpl.ownerId = @ownerId and bpl.ownerType = @ownerType and bpl.principalId = a.containerId inner join tblManufacturers mfr with (nolock) on mfr.mfr_id = bpl.mfrId where a.containerType = 'BPL' union all select a.*,cast (null as varchar ) as partnerName, cast( null as integer ) as partnerId, cast( null as varchar ) as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a where coalesce( a.containerType, '???') not in ( 'BPL', 'OPR', 'CDR' ) ), assetsWithUser as ( select a.*, m.email updateUserEmail, m.firstName + ' ' + m.lastName as updateUserFullName, case when len(isnull(a.linked_prd,'')) > 0 or len(isnull(a.linked_sku,'')) > 0 then 'Y' else 'N' end as prdLink, case when len(isnull(a.linked_opr,'')) > 0 then 'Y' else 'N' end as oprLink, case when len(isnull(a.linked_cdr,'')) > 0 then 'Y' else 'N' end as cdrLink, case when len(isnull(a.linked_mfr,'')) > 0 then 'Y' else 'N' end as mfrLink from assetsWithPartner as a left outer join tbl_Fspro_members m with (nolock) on m.ownerId > 0 and m.fspro_userId = a.updateUserId ) , assetsWithUser_mappedData as ( select * from assetsWithUser ) , assetsWithUser_preData as ( select * from assetsWithUser_mappedData with (nolock) where ( ( 1 = 1 ) and ( ( ( [isPublished] = 'Y' ) or ( [isPublished] = 'F' ) ) ) ) ) , assetsWithUser_data as ( select * from assetsWithUser_preData ) , assetsWithUser_return as ( select * from assetsWithUser_data where 1 = 1 order by docTitle offset 0 rows fetch next 50 rows only ) , assetsWithUser_result as ( select ( select count(*) from assetsWithUser_data ) as totalCount, ( select * from assetsWithUser_return for json auto, include_null_values ) as data ) select * from assetsWithUser_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER -- 2026-04-16 17:39:34.511 -- 0 {ts '2026-04-16 17:39:41'} SQL declare @ownerId int = 207 ; declare @ownerType varchar(3) = 'MFR' ; declare @contentPrefix varchar(128) = 'http://dev.fsenablers.com/FSLContent/' ; declare @todayDate datetime = ?; with docCategories as ( SELECT cats.attrID as categoryId, cats.attrDescription AS categoryName FROM tbl_ORG_Attributes cats with (nolock) WHERE cats.OwnerID = @ownerId AND cats.FSL_TableCode = @ownerType AND cats.attrTypeID = 31 ), rawAssets as ( select null contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, cast( null as varchar ) docTitle, cast( null as int) as categoryId, cast( null as int ) contentId, cast( null as int) fileId, 0.00 fileSize, null fileExt, null docDescription, cast( null as datetime ) publishDate, cast( null as datetime ) unpublishDate, 'N' isPublished, cast( null as datetime ) uploadDate, cast( null as datetime ) updateDate, cast( null as int ) updateUserId, 0 restricted, -1 thumbnailId, -1 highresCntId, 99 as sortRank , 0 as freshness , 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr, cast( null as varchar ) as assetSelectionKey, 0 as selectedRecord , 'N' as internalOnly, '' as recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds union all select 'DOC' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, d.docTitle, d.repCategoryId as categoryId, d.contentId, d.docId as fileId, CAST((d.fileSize/1024) AS float) as fileSize , d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, case when d.publishDate is not null and d.publishDate > @todayDate then 'F' when d.publishDate is not null and d.unpublishDate is not null and @todayDate >= d.publishDate and @todayDate <= d.unpublishDate then 'Y' when d.publishDate is not null and d.unpublishDate is null and d.publishDate <= @todayDate then 'Y' when d.publishDate is null then 'N' else 'N' end isPublished, d.uploadDate, d.lastUpdatedDate updateDate, d.lastUpdatedBy updateUserId, 0 restricted, thumbnailId, -1 highresCntId, sortRank , coalesce(dateDiff(day, d.lastUpdatedDate, @todayDate),0) as freshness, case when d.privateYesNo = 1 then 'Y' else 'N' end privateYesNo, ou.firstname + ' ' + ou.lastname as uploadedByFullName, d.keyWords, d.unpublishToDo, d.internalNotes , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END), ',') AS linked_opr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END), ',') AS linked_prd , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END), ',') AS linked_sku , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END), ',') AS linked_cdr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END), ',') AS linked_mfr , cast(concat('DOC',':',d.docId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , d.internalOnly, d.recordSource , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 23 THEN li.child_attrID ELSE NULL END), ',') AS permissionGrpIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 7 THEN li.child_attrID ELSE NULL END), ',') AS linked_brokerIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 31 THEN li.child_attrID ELSE NULL END), ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = d.docId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 34 THEN li.child_attrID ELSE NULL END), ',') AS linked_UserMemberIds from tbl_DOC_Documents d with (nolock) LEFT OUTER JOIN tbl_FSPRO_Members ou with (nolock) ON d.AuthorUserId = ou.fspro_userId left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = d.docId and li.child_attrTypeID in (41,59,60,63,42,23,7,31,34) and li.mstr_attrTypeID = 30 left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('DOC',':',d.docId) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType GROUP BY d.docTitle, d.repCategoryId, d.contentId, d.docId,d.fileSize, d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, d.uploadDate, d.lastUpdatedDate, d.lastUpdatedBy,thumbnailId, sortRank , d.privateYesNo , ou.firstname,ou.lastname, d.keyWords, d.unpublishToDo, d.internalNotes,ast.assetSelectionKey, d.internalOnly, d.recordSource union all select 'IMG' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, i.imgTitle as docTitle, i.categoryId, i.contentId, i.imgId as fileId, CAST((i.fileSize/1024) AS float) as fileSize, i.fileExt, i.imgDescription as docDescription, i.publishDate, i.unpublishDate, case when i.publishDate is not null and i.publishDate > @todayDate then 'F' when i.publishDate is not null and i.unpublishDate is not null and @todayDate >= i.publishDate and @todayDate <= i.unpublishDate then 'Y' when i.publishDate is not null and i.unpublishDate is null and i.publishDate <= @todayDate then 'Y' when i.publishDate is null then 'N' else 'N' end isPublished, i.uploadDate, i.lastUpdatedDate updateDate, i.lastUpdatedBy updateUserId, 0 restricted, -1 thumbnailId, i.highresCntId, i.sortRank, coalesce(dateDiff(day, lastUpdatedDate, @todayDate),0) as freshness, case when i.privateYesNo = 1 then 'Y' else 'N' end privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , STRING_AGG(CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END, ',') AS linked_opr , STRING_AGG(CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END, ',') AS linked_prd , STRING_AGG(CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END, ',') AS linked_sku , STRING_AGG(CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END, ',') AS linked_cdr , STRING_AGG(CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END, ',') AS linked_mfr , cast(concat('IMG',':',i.imgId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , i.internalOnly, i.recordSource , STRING_AGG(CASE WHEN c.child_attrTypeID = 23 THEN c.child_attrID ELSE NULL END, ',') AS permissionGrpIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 7 THEN c.child_attrID ELSE NULL END, ',') AS linked_brokerIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 31 THEN c.child_attrID ELSE NULL END, ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = i.imgId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CASE WHEN c.child_attrTypeID = 34 THEN c.child_attrID ELSE NULL END, ',') AS linked_UserMemberIds from tbl_IMG_Images i with (nolock) left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = i.imgId and li.child_attrTypeID in (41,59,60,63,42) and li.mstr_attrTypeID = 33 left outer JOIN tbl_ORG_AttributeLinks c ON c.mstr_attrID = i.imgId and c.child_attrTypeID in (23,7,31,34) and c.mstr_attrTypeID = 30 left outer join tbl_DOC_Categories dc with (nolock) ON c.child_attrTypeID = 31 AND dc.categoryId = c.child_attrID AND dc.ownerId = @ownerId AND dc.fsl_tablecode = @ownerType left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('IMG',':',i.imgId) where i.ownerId = @ownerId and i.fsl_tablecode = @ownerType GROUP BY i.imgTitle, i.CategoryID, i.contentId, i.imgId, i.fileSize, i.fileExt, i.imgDescription, i.publishDate, i.unpublishDate, i.uploadDate, i.lastUpdatedDate, i.lastUpdatedBy, i.highresCntId, i.sortRank, i.privateYesNo,ast.assetSelectionKey, i.internalOnly, i.recordSource ), filteredRawAssets as ( select ra.* from rawAssets ra where (1=1 or ra.linked_brokerIds in (?) ) ) , assetsWithContentURL1 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL, c.fileName as docFileName, lub.firstname + ' ' + lub.lastname as lastUpdatedByFullName from filteredRawAssets ra LEFT OUTER JOIN tbl_FSPRO_Members lub with (nolock) ON ra.updateUserId = lub.fspro_userId inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) , assetsWithContentURL2 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as thumbnailURL, c.fileName as thnFileName from assetsWithContentURL1 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.thumbnailId and c.contentId != 755 ) , assetsWithContentURL3 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as highresURL, c.fileName as highresCntFileName from assetsWithContentURL2 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.highresCntId and c.contentId != 755 ) , assets as ( select a.*, case when right( a.contentURL, charindex( '.', reverse( a.contentURL )) - 1 ) in ( 'BMP','GIF','JPEG','JPG','PNG','TIF','TIFF','WBMP' ) then left( a.contentURL, len( a.contentURL ) - charindex( '.', reverse( a.contentURL ) )) + '_50' + right( a.contentURL, charindex( '.', reverse( a.contentURL )) ) else null end previewURL from assetsWithContentURL3 a ) , categories as ( select 'OPR' contentDomain, categoryId, categoryDescription from tbl_OPR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'CDR' contentDomain, categoryId, categoryDescription from tbl_CDR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'BPL' contentDomain, categoryId, categoryDescription from tbl_BPL_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'DOC' contentDomain, categoryId, categoryName as categoryDescription from tbl_DOC_Categories with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType ) , assetsWithCategory as ( select a.*, c.categoryDescription from assets a left outer join categories c on c.contentDomain = a.contentDomain and c.categoryId = a.categoryId ) ,industryCategories as ( SELECT categoryId, categoryDescription FROM tbl_PRD_RepositoryCategories with (nolock) WHERE containerType = 'SKU' AND ownerID = 0 AND ownerType = '*' AND left(categoryDescription, 3) <> 'GS1' ) , assetsWithIndustryCategory as ( select a.*, ic.categoryDescription as industryCategoryDescription from assetsWithCategory a left outer join industryCategories ic on ic.categoryId = a.categoryId ) , assetsWithPartner as ( select a.*, opr.companyName as partnerName, a.containerId as partnerId, a.containerType as partnerType, opr.territoryId from assetsWithIndustryCategory a inner join tbl_OPR_CLientOperators opr with (nolock) on opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = a.containerId where a.containerType = 'OPR' union all select a.*, cdr.cdr_dstName as partnerName, a.containerId as partnerId, a.containerType as partnerType, cdr.cdr_territoryId as territoryId from assetsWithIndustryCategory a inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = @ownerId and cdr.fsl_tablecode = @ownerType and cdr.cdr_recordId = a.containerId where a.containerType = 'CDR' union all select a.*, mfr.mfr_name as partnerName, mfr.mfr_id as partnerId, 'MFR' as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a inner join tbl_BRO_Principals bpl with (nolock) on bpl.ownerId = @ownerId and bpl.ownerType = @ownerType and bpl.principalId = a.containerId inner join tblManufacturers mfr with (nolock) on mfr.mfr_id = bpl.mfrId where a.containerType = 'BPL' union all select a.*,cast (null as varchar ) as partnerName, cast( null as integer ) as partnerId, cast( null as varchar ) as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a where coalesce( a.containerType, '???') not in ( 'BPL', 'OPR', 'CDR' ) ), assetsWithUser as ( select a.*, m.email updateUserEmail, m.firstName + ' ' + m.lastName as updateUserFullName, case when len(isnull(a.linked_prd,'')) > 0 or len(isnull(a.linked_sku,'')) > 0 then 'Y' else 'N' end as prdLink, case when len(isnull(a.linked_opr,'')) > 0 then 'Y' else 'N' end as oprLink, case when len(isnull(a.linked_cdr,'')) > 0 then 'Y' else 'N' end as cdrLink, case when len(isnull(a.linked_mfr,'')) > 0 then 'Y' else 'N' end as mfrLink from assetsWithPartner as a left outer join tbl_Fspro_members m with (nolock) on m.ownerId > 0 and m.fspro_userId = a.updateUserId ) , assetsWithUser_mappedData as ( select * from assetsWithUser ) , assetsWithUser_preData as ( select * from assetsWithUser_mappedData with (nolock) where ( ( [fileId] = '1' ) and ( ( 1 = 1 ) and ( ( ( [isPublished] = 'Y' ) or ( [isPublished] = 'F' ) ) ) ) ) ) , assetsWithUser_data as ( select * from assetsWithUser_preData ) , assetsWithUser_return as ( select * from assetsWithUser_data where 1 = 1 order by docTitle offset 0 rows fetch next 50 rows only ) , assetsWithUser_result as ( select ( select count(*) from assetsWithUser_data ) as totalCount, ( select * from assetsWithUser_return for json auto, include_null_values ) as data ) select * from assetsWithUser_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER -- 2026-04-16 17:39:40.989 -- 0 {ts '2026-04-16 17:39:42'} SQL declare @ownerId int = 207 ; declare @ownerType varchar(3) = 'MFR' ; declare @contentPrefix varchar(128) = 'http://dev.fsenablers.com/FSLContent/' ; declare @todayDate datetime = ?; with docCategories as ( SELECT cats.attrID as categoryId, cats.attrDescription AS categoryName FROM tbl_ORG_Attributes cats with (nolock) WHERE cats.OwnerID = @ownerId AND cats.FSL_TableCode = @ownerType AND cats.attrTypeID = 31 ), rawAssets as ( select null contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, cast( null as varchar ) docTitle, cast( null as int) as categoryId, cast( null as int ) contentId, cast( null as int) fileId, 0.00 fileSize, null fileExt, null docDescription, cast( null as datetime ) publishDate, cast( null as datetime ) unpublishDate, 'N' isPublished, cast( null as datetime ) uploadDate, cast( null as datetime ) updateDate, cast( null as int ) updateUserId, 0 restricted, -1 thumbnailId, -1 highresCntId, 99 as sortRank , 0 as freshness , 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr, cast( null as varchar ) as assetSelectionKey, 0 as selectedRecord , 'N' as internalOnly, '' as recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds union all select 'DOC' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, d.docTitle, d.repCategoryId as categoryId, d.contentId, d.docId as fileId, CAST((d.fileSize/1024) AS float) as fileSize , d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, case when d.publishDate is not null and d.publishDate > @todayDate then 'F' when d.publishDate is not null and d.unpublishDate is not null and @todayDate >= d.publishDate and @todayDate <= d.unpublishDate then 'Y' when d.publishDate is not null and d.unpublishDate is null and d.publishDate <= @todayDate then 'Y' when d.publishDate is null then 'N' else 'N' end isPublished, d.uploadDate, d.lastUpdatedDate updateDate, d.lastUpdatedBy updateUserId, 0 restricted, thumbnailId, -1 highresCntId, sortRank , coalesce(dateDiff(day, d.lastUpdatedDate, @todayDate),0) as freshness, case when d.privateYesNo = 1 then 'Y' else 'N' end privateYesNo, ou.firstname + ' ' + ou.lastname as uploadedByFullName, d.keyWords, d.unpublishToDo, d.internalNotes , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END), ',') AS linked_opr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END), ',') AS linked_prd , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END), ',') AS linked_sku , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END), ',') AS linked_cdr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END), ',') AS linked_mfr , cast(concat('DOC',':',d.docId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , d.internalOnly, d.recordSource , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 23 THEN li.child_attrID ELSE NULL END), ',') AS permissionGrpIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 7 THEN li.child_attrID ELSE NULL END), ',') AS linked_brokerIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 31 THEN li.child_attrID ELSE NULL END), ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = d.docId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 34 THEN li.child_attrID ELSE NULL END), ',') AS linked_UserMemberIds from tbl_DOC_Documents d with (nolock) LEFT OUTER JOIN tbl_FSPRO_Members ou with (nolock) ON d.AuthorUserId = ou.fspro_userId left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = d.docId and li.child_attrTypeID in (41,59,60,63,42,23,7,31,34) and li.mstr_attrTypeID = 30 left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('DOC',':',d.docId) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType GROUP BY d.docTitle, d.repCategoryId, d.contentId, d.docId,d.fileSize, d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, d.uploadDate, d.lastUpdatedDate, d.lastUpdatedBy,thumbnailId, sortRank , d.privateYesNo , ou.firstname,ou.lastname, d.keyWords, d.unpublishToDo, d.internalNotes,ast.assetSelectionKey, d.internalOnly, d.recordSource union all select 'IMG' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, i.imgTitle as docTitle, i.categoryId, i.contentId, i.imgId as fileId, CAST((i.fileSize/1024) AS float) as fileSize, i.fileExt, i.imgDescription as docDescription, i.publishDate, i.unpublishDate, case when i.publishDate is not null and i.publishDate > @todayDate then 'F' when i.publishDate is not null and i.unpublishDate is not null and @todayDate >= i.publishDate and @todayDate <= i.unpublishDate then 'Y' when i.publishDate is not null and i.unpublishDate is null and i.publishDate <= @todayDate then 'Y' when i.publishDate is null then 'N' else 'N' end isPublished, i.uploadDate, i.lastUpdatedDate updateDate, i.lastUpdatedBy updateUserId, 0 restricted, -1 thumbnailId, i.highresCntId, i.sortRank, coalesce(dateDiff(day, lastUpdatedDate, @todayDate),0) as freshness, case when i.privateYesNo = 1 then 'Y' else 'N' end privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , STRING_AGG(CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END, ',') AS linked_opr , STRING_AGG(CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END, ',') AS linked_prd , STRING_AGG(CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END, ',') AS linked_sku , STRING_AGG(CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END, ',') AS linked_cdr , STRING_AGG(CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END, ',') AS linked_mfr , cast(concat('IMG',':',i.imgId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , i.internalOnly, i.recordSource , STRING_AGG(CASE WHEN c.child_attrTypeID = 23 THEN c.child_attrID ELSE NULL END, ',') AS permissionGrpIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 7 THEN c.child_attrID ELSE NULL END, ',') AS linked_brokerIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 31 THEN c.child_attrID ELSE NULL END, ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = i.imgId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CASE WHEN c.child_attrTypeID = 34 THEN c.child_attrID ELSE NULL END, ',') AS linked_UserMemberIds from tbl_IMG_Images i with (nolock) left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = i.imgId and li.child_attrTypeID in (41,59,60,63,42) and li.mstr_attrTypeID = 33 left outer JOIN tbl_ORG_AttributeLinks c ON c.mstr_attrID = i.imgId and c.child_attrTypeID in (23,7,31,34) and c.mstr_attrTypeID = 30 left outer join tbl_DOC_Categories dc with (nolock) ON c.child_attrTypeID = 31 AND dc.categoryId = c.child_attrID AND dc.ownerId = @ownerId AND dc.fsl_tablecode = @ownerType left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('IMG',':',i.imgId) where i.ownerId = @ownerId and i.fsl_tablecode = @ownerType GROUP BY i.imgTitle, i.CategoryID, i.contentId, i.imgId, i.fileSize, i.fileExt, i.imgDescription, i.publishDate, i.unpublishDate, i.uploadDate, i.lastUpdatedDate, i.lastUpdatedBy, i.highresCntId, i.sortRank, i.privateYesNo,ast.assetSelectionKey, i.internalOnly, i.recordSource ), filteredRawAssets as ( select ra.* from rawAssets ra where (1=1 or ra.linked_brokerIds in (?) ) ) , assetsWithContentURL1 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL, c.fileName as docFileName, lub.firstname + ' ' + lub.lastname as lastUpdatedByFullName from filteredRawAssets ra LEFT OUTER JOIN tbl_FSPRO_Members lub with (nolock) ON ra.updateUserId = lub.fspro_userId inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) , assetsWithContentURL2 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as thumbnailURL, c.fileName as thnFileName from assetsWithContentURL1 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.thumbnailId and c.contentId != 755 ) , assetsWithContentURL3 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as highresURL, c.fileName as highresCntFileName from assetsWithContentURL2 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.highresCntId and c.contentId != 755 ) , assets as ( select a.*, case when right( a.contentURL, charindex( '.', reverse( a.contentURL )) - 1 ) in ( 'BMP','GIF','JPEG','JPG','PNG','TIF','TIFF','WBMP' ) then left( a.contentURL, len( a.contentURL ) - charindex( '.', reverse( a.contentURL ) )) + '_50' + right( a.contentURL, charindex( '.', reverse( a.contentURL )) ) else null end previewURL from assetsWithContentURL3 a ) , categories as ( select 'OPR' contentDomain, categoryId, categoryDescription from tbl_OPR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'CDR' contentDomain, categoryId, categoryDescription from tbl_CDR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'BPL' contentDomain, categoryId, categoryDescription from tbl_BPL_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'DOC' contentDomain, categoryId, categoryName as categoryDescription from tbl_DOC_Categories with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType ) , assetsWithCategory as ( select a.*, c.categoryDescription from assets a left outer join categories c on c.contentDomain = a.contentDomain and c.categoryId = a.categoryId ) ,industryCategories as ( SELECT categoryId, categoryDescription FROM tbl_PRD_RepositoryCategories with (nolock) WHERE containerType = 'SKU' AND ownerID = 0 AND ownerType = '*' AND left(categoryDescription, 3) <> 'GS1' ) , assetsWithIndustryCategory as ( select a.*, ic.categoryDescription as industryCategoryDescription from assetsWithCategory a left outer join industryCategories ic on ic.categoryId = a.categoryId ) , assetsWithPartner as ( select a.*, opr.companyName as partnerName, a.containerId as partnerId, a.containerType as partnerType, opr.territoryId from assetsWithIndustryCategory a inner join tbl_OPR_CLientOperators opr with (nolock) on opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = a.containerId where a.containerType = 'OPR' union all select a.*, cdr.cdr_dstName as partnerName, a.containerId as partnerId, a.containerType as partnerType, cdr.cdr_territoryId as territoryId from assetsWithIndustryCategory a inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = @ownerId and cdr.fsl_tablecode = @ownerType and cdr.cdr_recordId = a.containerId where a.containerType = 'CDR' union all select a.*, mfr.mfr_name as partnerName, mfr.mfr_id as partnerId, 'MFR' as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a inner join tbl_BRO_Principals bpl with (nolock) on bpl.ownerId = @ownerId and bpl.ownerType = @ownerType and bpl.principalId = a.containerId inner join tblManufacturers mfr with (nolock) on mfr.mfr_id = bpl.mfrId where a.containerType = 'BPL' union all select a.*,cast (null as varchar ) as partnerName, cast( null as integer ) as partnerId, cast( null as varchar ) as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a where coalesce( a.containerType, '???') not in ( 'BPL', 'OPR', 'CDR' ) ), assetsWithUser as ( select a.*, m.email updateUserEmail, m.firstName + ' ' + m.lastName as updateUserFullName, case when len(isnull(a.linked_prd,'')) > 0 or len(isnull(a.linked_sku,'')) > 0 then 'Y' else 'N' end as prdLink, case when len(isnull(a.linked_opr,'')) > 0 then 'Y' else 'N' end as oprLink, case when len(isnull(a.linked_cdr,'')) > 0 then 'Y' else 'N' end as cdrLink, case when len(isnull(a.linked_mfr,'')) > 0 then 'Y' else 'N' end as mfrLink from assetsWithPartner as a left outer join tbl_Fspro_members m with (nolock) on m.ownerId > 0 and m.fspro_userId = a.updateUserId ) , assetsWithUser_mappedData as ( select * from assetsWithUser ) , assetsWithUser_preData as ( select * from assetsWithUser_mappedData with (nolock) where ( ( [fileId] = '180' ) and ( ( 1 = 1 ) and ( ( ( [isPublished] = 'Y' ) or ( [isPublished] = 'F' ) ) ) ) ) ) , assetsWithUser_data as ( select * from assetsWithUser_preData ) , assetsWithUser_return as ( select * from assetsWithUser_data where 1 = 1 order by docTitle offset 0 rows fetch next 50 rows only ) , assetsWithUser_result as ( select ( select count(*) from assetsWithUser_data ) as totalCount, ( select * from assetsWithUser_return for json auto, include_null_values ) as data ) select * from assetsWithUser_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER -- 2026-04-16 17:39:42.008 -- 0 {ts '2026-04-16 17:39:44'} SQL declare @ownerId int = 207 ; declare @ownerType varchar(3) = 'MFR' ; declare @contentPrefix varchar(128) = 'http://dev.fsenablers.com/FSLContent/' ; declare @todayDate datetime = ?; with docCategories as ( SELECT cats.attrID as categoryId, cats.attrDescription AS categoryName FROM tbl_ORG_Attributes cats with (nolock) WHERE cats.OwnerID = @ownerId AND cats.FSL_TableCode = @ownerType AND cats.attrTypeID = 31 ), rawAssets as ( select null contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, cast( null as varchar ) docTitle, cast( null as int) as categoryId, cast( null as int ) contentId, cast( null as int) fileId, 0.00 fileSize, null fileExt, null docDescription, cast( null as datetime ) publishDate, cast( null as datetime ) unpublishDate, 'N' isPublished, cast( null as datetime ) uploadDate, cast( null as datetime ) updateDate, cast( null as int ) updateUserId, 0 restricted, -1 thumbnailId, -1 highresCntId, 99 as sortRank , 0 as freshness , 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr, cast( null as varchar ) as assetSelectionKey, 0 as selectedRecord , 'N' as internalOnly, '' as recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds union all select 'DOC' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, d.docTitle, d.repCategoryId as categoryId, d.contentId, d.docId as fileId, CAST((d.fileSize/1024) AS float) as fileSize , d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, case when d.publishDate is not null and d.publishDate > @todayDate then 'F' when d.publishDate is not null and d.unpublishDate is not null and @todayDate >= d.publishDate and @todayDate <= d.unpublishDate then 'Y' when d.publishDate is not null and d.unpublishDate is null and d.publishDate <= @todayDate then 'Y' when d.publishDate is null then 'N' else 'N' end isPublished, d.uploadDate, d.lastUpdatedDate updateDate, d.lastUpdatedBy updateUserId, 0 restricted, thumbnailId, -1 highresCntId, sortRank , coalesce(dateDiff(day, d.lastUpdatedDate, @todayDate),0) as freshness, case when d.privateYesNo = 1 then 'Y' else 'N' end privateYesNo, ou.firstname + ' ' + ou.lastname as uploadedByFullName, d.keyWords, d.unpublishToDo, d.internalNotes , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END), ',') AS linked_opr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END), ',') AS linked_prd , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END), ',') AS linked_sku , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END), ',') AS linked_cdr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END), ',') AS linked_mfr , cast(concat('DOC',':',d.docId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , d.internalOnly, d.recordSource , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 23 THEN li.child_attrID ELSE NULL END), ',') AS permissionGrpIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 7 THEN li.child_attrID ELSE NULL END), ',') AS linked_brokerIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 31 THEN li.child_attrID ELSE NULL END), ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = d.docId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 34 THEN li.child_attrID ELSE NULL END), ',') AS linked_UserMemberIds from tbl_DOC_Documents d with (nolock) LEFT OUTER JOIN tbl_FSPRO_Members ou with (nolock) ON d.AuthorUserId = ou.fspro_userId left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = d.docId and li.child_attrTypeID in (41,59,60,63,42,23,7,31,34) and li.mstr_attrTypeID = 30 left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('DOC',':',d.docId) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType GROUP BY d.docTitle, d.repCategoryId, d.contentId, d.docId,d.fileSize, d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, d.uploadDate, d.lastUpdatedDate, d.lastUpdatedBy,thumbnailId, sortRank , d.privateYesNo , ou.firstname,ou.lastname, d.keyWords, d.unpublishToDo, d.internalNotes,ast.assetSelectionKey, d.internalOnly, d.recordSource union all select 'IMG' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, i.imgTitle as docTitle, i.categoryId, i.contentId, i.imgId as fileId, CAST((i.fileSize/1024) AS float) as fileSize, i.fileExt, i.imgDescription as docDescription, i.publishDate, i.unpublishDate, case when i.publishDate is not null and i.publishDate > @todayDate then 'F' when i.publishDate is not null and i.unpublishDate is not null and @todayDate >= i.publishDate and @todayDate <= i.unpublishDate then 'Y' when i.publishDate is not null and i.unpublishDate is null and i.publishDate <= @todayDate then 'Y' when i.publishDate is null then 'N' else 'N' end isPublished, i.uploadDate, i.lastUpdatedDate updateDate, i.lastUpdatedBy updateUserId, 0 restricted, -1 thumbnailId, i.highresCntId, i.sortRank, coalesce(dateDiff(day, lastUpdatedDate, @todayDate),0) as freshness, case when i.privateYesNo = 1 then 'Y' else 'N' end privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , STRING_AGG(CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END, ',') AS linked_opr , STRING_AGG(CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END, ',') AS linked_prd , STRING_AGG(CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END, ',') AS linked_sku , STRING_AGG(CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END, ',') AS linked_cdr , STRING_AGG(CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END, ',') AS linked_mfr , cast(concat('IMG',':',i.imgId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , i.internalOnly, i.recordSource , STRING_AGG(CASE WHEN c.child_attrTypeID = 23 THEN c.child_attrID ELSE NULL END, ',') AS permissionGrpIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 7 THEN c.child_attrID ELSE NULL END, ',') AS linked_brokerIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 31 THEN c.child_attrID ELSE NULL END, ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = i.imgId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CASE WHEN c.child_attrTypeID = 34 THEN c.child_attrID ELSE NULL END, ',') AS linked_UserMemberIds from tbl_IMG_Images i with (nolock) left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = i.imgId and li.child_attrTypeID in (41,59,60,63,42) and li.mstr_attrTypeID = 33 left outer JOIN tbl_ORG_AttributeLinks c ON c.mstr_attrID = i.imgId and c.child_attrTypeID in (23,7,31,34) and c.mstr_attrTypeID = 30 left outer join tbl_DOC_Categories dc with (nolock) ON c.child_attrTypeID = 31 AND dc.categoryId = c.child_attrID AND dc.ownerId = @ownerId AND dc.fsl_tablecode = @ownerType left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('IMG',':',i.imgId) where i.ownerId = @ownerId and i.fsl_tablecode = @ownerType GROUP BY i.imgTitle, i.CategoryID, i.contentId, i.imgId, i.fileSize, i.fileExt, i.imgDescription, i.publishDate, i.unpublishDate, i.uploadDate, i.lastUpdatedDate, i.lastUpdatedBy, i.highresCntId, i.sortRank, i.privateYesNo,ast.assetSelectionKey, i.internalOnly, i.recordSource ), filteredRawAssets as ( select ra.* from rawAssets ra where (1=1 or ra.linked_brokerIds in (?) ) ) , assetsWithContentURL1 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL, c.fileName as docFileName, lub.firstname + ' ' + lub.lastname as lastUpdatedByFullName from filteredRawAssets ra LEFT OUTER JOIN tbl_FSPRO_Members lub with (nolock) ON ra.updateUserId = lub.fspro_userId inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) , assetsWithContentURL2 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as thumbnailURL, c.fileName as thnFileName from assetsWithContentURL1 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.thumbnailId and c.contentId != 755 ) , assetsWithContentURL3 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as highresURL, c.fileName as highresCntFileName from assetsWithContentURL2 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.highresCntId and c.contentId != 755 ) , assets as ( select a.*, case when right( a.contentURL, charindex( '.', reverse( a.contentURL )) - 1 ) in ( 'BMP','GIF','JPEG','JPG','PNG','TIF','TIFF','WBMP' ) then left( a.contentURL, len( a.contentURL ) - charindex( '.', reverse( a.contentURL ) )) + '_50' + right( a.contentURL, charindex( '.', reverse( a.contentURL )) ) else null end previewURL from assetsWithContentURL3 a ) , categories as ( select 'OPR' contentDomain, categoryId, categoryDescription from tbl_OPR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'CDR' contentDomain, categoryId, categoryDescription from tbl_CDR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'BPL' contentDomain, categoryId, categoryDescription from tbl_BPL_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'DOC' contentDomain, categoryId, categoryName as categoryDescription from tbl_DOC_Categories with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType ) , assetsWithCategory as ( select a.*, c.categoryDescription from assets a left outer join categories c on c.contentDomain = a.contentDomain and c.categoryId = a.categoryId ) ,industryCategories as ( SELECT categoryId, categoryDescription FROM tbl_PRD_RepositoryCategories with (nolock) WHERE containerType = 'SKU' AND ownerID = 0 AND ownerType = '*' AND left(categoryDescription, 3) <> 'GS1' ) , assetsWithIndustryCategory as ( select a.*, ic.categoryDescription as industryCategoryDescription from assetsWithCategory a left outer join industryCategories ic on ic.categoryId = a.categoryId ) , assetsWithPartner as ( select a.*, opr.companyName as partnerName, a.containerId as partnerId, a.containerType as partnerType, opr.territoryId from assetsWithIndustryCategory a inner join tbl_OPR_CLientOperators opr with (nolock) on opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = a.containerId where a.containerType = 'OPR' union all select a.*, cdr.cdr_dstName as partnerName, a.containerId as partnerId, a.containerType as partnerType, cdr.cdr_territoryId as territoryId from assetsWithIndustryCategory a inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = @ownerId and cdr.fsl_tablecode = @ownerType and cdr.cdr_recordId = a.containerId where a.containerType = 'CDR' union all select a.*, mfr.mfr_name as partnerName, mfr.mfr_id as partnerId, 'MFR' as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a inner join tbl_BRO_Principals bpl with (nolock) on bpl.ownerId = @ownerId and bpl.ownerType = @ownerType and bpl.principalId = a.containerId inner join tblManufacturers mfr with (nolock) on mfr.mfr_id = bpl.mfrId where a.containerType = 'BPL' union all select a.*,cast (null as varchar ) as partnerName, cast( null as integer ) as partnerId, cast( null as varchar ) as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a where coalesce( a.containerType, '???') not in ( 'BPL', 'OPR', 'CDR' ) ), assetsWithUser as ( select a.*, m.email updateUserEmail, m.firstName + ' ' + m.lastName as updateUserFullName, case when len(isnull(a.linked_prd,'')) > 0 or len(isnull(a.linked_sku,'')) > 0 then 'Y' else 'N' end as prdLink, case when len(isnull(a.linked_opr,'')) > 0 then 'Y' else 'N' end as oprLink, case when len(isnull(a.linked_cdr,'')) > 0 then 'Y' else 'N' end as cdrLink, case when len(isnull(a.linked_mfr,'')) > 0 then 'Y' else 'N' end as mfrLink from assetsWithPartner as a left outer join tbl_Fspro_members m with (nolock) on m.ownerId > 0 and m.fspro_userId = a.updateUserId ) , assetsWithUser_mappedData as ( select * from assetsWithUser ) , assetsWithUser_preData as ( select * from assetsWithUser_mappedData with (nolock) where ( ( 1 = 1 ) and ( ( ( [isPublished] = 'Y' ) or ( [isPublished] = 'F' ) ) ) ) ) , assetsWithUser_data as ( select * from assetsWithUser_preData ) , assetsWithUser_return as ( select * from assetsWithUser_data where 1 = 1 order by docTitle offset 0 rows fetch next 50 rows only ) , assetsWithUser_result as ( select ( select count(*) from assetsWithUser_data ) as totalCount, ( select * from assetsWithUser_return for json auto, include_null_values ) as data ) select * from assetsWithUser_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER -- 2026-04-16 17:39:44.675 -- 0 {ts '2026-04-16 17:39:48'} SQL declare @ownerId int = 207 ; declare @ownerType varchar(3) = 'MFR' ; declare @contentPrefix varchar(128) = 'http://dev.fsenablers.com/FSLContent/' ; declare @todayDate datetime = ?; with docCategories as ( SELECT cats.attrID as categoryId, cats.attrDescription AS categoryName FROM tbl_ORG_Attributes cats with (nolock) WHERE cats.OwnerID = @ownerId AND cats.FSL_TableCode = @ownerType AND cats.attrTypeID = 31 ), rawAssets as ( select null contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, cast( null as varchar ) docTitle, cast( null as int) as categoryId, cast( null as int ) contentId, cast( null as int) fileId, 0.00 fileSize, null fileExt, null docDescription, cast( null as datetime ) publishDate, cast( null as datetime ) unpublishDate, 'N' isPublished, cast( null as datetime ) uploadDate, cast( null as datetime ) updateDate, cast( null as int ) updateUserId, 0 restricted, -1 thumbnailId, -1 highresCntId, 99 as sortRank , 0 as freshness , 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr, cast( null as varchar ) as assetSelectionKey, 0 as selectedRecord , 'N' as internalOnly, '' as recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds union all select 'DOC' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, d.docTitle, d.repCategoryId as categoryId, d.contentId, d.docId as fileId, CAST((d.fileSize/1024) AS float) as fileSize , d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, case when d.publishDate is not null and d.publishDate > @todayDate then 'F' when d.publishDate is not null and d.unpublishDate is not null and @todayDate >= d.publishDate and @todayDate <= d.unpublishDate then 'Y' when d.publishDate is not null and d.unpublishDate is null and d.publishDate <= @todayDate then 'Y' when d.publishDate is null then 'N' else 'N' end isPublished, d.uploadDate, d.lastUpdatedDate updateDate, d.lastUpdatedBy updateUserId, 0 restricted, thumbnailId, -1 highresCntId, sortRank , coalesce(dateDiff(day, d.lastUpdatedDate, @todayDate),0) as freshness, case when d.privateYesNo = 1 then 'Y' else 'N' end privateYesNo, ou.firstname + ' ' + ou.lastname as uploadedByFullName, d.keyWords, d.unpublishToDo, d.internalNotes , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END), ',') AS linked_opr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END), ',') AS linked_prd , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END), ',') AS linked_sku , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END), ',') AS linked_cdr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END), ',') AS linked_mfr , cast(concat('DOC',':',d.docId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , d.internalOnly, d.recordSource , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 23 THEN li.child_attrID ELSE NULL END), ',') AS permissionGrpIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 7 THEN li.child_attrID ELSE NULL END), ',') AS linked_brokerIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 31 THEN li.child_attrID ELSE NULL END), ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = d.docId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 34 THEN li.child_attrID ELSE NULL END), ',') AS linked_UserMemberIds from tbl_DOC_Documents d with (nolock) LEFT OUTER JOIN tbl_FSPRO_Members ou with (nolock) ON d.AuthorUserId = ou.fspro_userId left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = d.docId and li.child_attrTypeID in (41,59,60,63,42,23,7,31,34) and li.mstr_attrTypeID = 30 left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('DOC',':',d.docId) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType GROUP BY d.docTitle, d.repCategoryId, d.contentId, d.docId,d.fileSize, d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, d.uploadDate, d.lastUpdatedDate, d.lastUpdatedBy,thumbnailId, sortRank , d.privateYesNo , ou.firstname,ou.lastname, d.keyWords, d.unpublishToDo, d.internalNotes,ast.assetSelectionKey, d.internalOnly, d.recordSource union all select 'IMG' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, i.imgTitle as docTitle, i.categoryId, i.contentId, i.imgId as fileId, CAST((i.fileSize/1024) AS float) as fileSize, i.fileExt, i.imgDescription as docDescription, i.publishDate, i.unpublishDate, case when i.publishDate is not null and i.publishDate > @todayDate then 'F' when i.publishDate is not null and i.unpublishDate is not null and @todayDate >= i.publishDate and @todayDate <= i.unpublishDate then 'Y' when i.publishDate is not null and i.unpublishDate is null and i.publishDate <= @todayDate then 'Y' when i.publishDate is null then 'N' else 'N' end isPublished, i.uploadDate, i.lastUpdatedDate updateDate, i.lastUpdatedBy updateUserId, 0 restricted, -1 thumbnailId, i.highresCntId, i.sortRank, coalesce(dateDiff(day, lastUpdatedDate, @todayDate),0) as freshness, case when i.privateYesNo = 1 then 'Y' else 'N' end privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , STRING_AGG(CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END, ',') AS linked_opr , STRING_AGG(CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END, ',') AS linked_prd , STRING_AGG(CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END, ',') AS linked_sku , STRING_AGG(CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END, ',') AS linked_cdr , STRING_AGG(CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END, ',') AS linked_mfr , cast(concat('IMG',':',i.imgId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , i.internalOnly, i.recordSource , STRING_AGG(CASE WHEN c.child_attrTypeID = 23 THEN c.child_attrID ELSE NULL END, ',') AS permissionGrpIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 7 THEN c.child_attrID ELSE NULL END, ',') AS linked_brokerIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 31 THEN c.child_attrID ELSE NULL END, ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = i.imgId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CASE WHEN c.child_attrTypeID = 34 THEN c.child_attrID ELSE NULL END, ',') AS linked_UserMemberIds from tbl_IMG_Images i with (nolock) left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = i.imgId and li.child_attrTypeID in (41,59,60,63,42) and li.mstr_attrTypeID = 33 left outer JOIN tbl_ORG_AttributeLinks c ON c.mstr_attrID = i.imgId and c.child_attrTypeID in (23,7,31,34) and c.mstr_attrTypeID = 30 left outer join tbl_DOC_Categories dc with (nolock) ON c.child_attrTypeID = 31 AND dc.categoryId = c.child_attrID AND dc.ownerId = @ownerId AND dc.fsl_tablecode = @ownerType left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('IMG',':',i.imgId) where i.ownerId = @ownerId and i.fsl_tablecode = @ownerType GROUP BY i.imgTitle, i.CategoryID, i.contentId, i.imgId, i.fileSize, i.fileExt, i.imgDescription, i.publishDate, i.unpublishDate, i.uploadDate, i.lastUpdatedDate, i.lastUpdatedBy, i.highresCntId, i.sortRank, i.privateYesNo,ast.assetSelectionKey, i.internalOnly, i.recordSource ), filteredRawAssets as ( select ra.* from rawAssets ra where (1=1 or ra.linked_brokerIds in (?) ) ) , assetsWithContentURL1 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL, c.fileName as docFileName, lub.firstname + ' ' + lub.lastname as lastUpdatedByFullName from filteredRawAssets ra LEFT OUTER JOIN tbl_FSPRO_Members lub with (nolock) ON ra.updateUserId = lub.fspro_userId inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) , assetsWithContentURL2 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as thumbnailURL, c.fileName as thnFileName from assetsWithContentURL1 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.thumbnailId and c.contentId != 755 ) , assetsWithContentURL3 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as highresURL, c.fileName as highresCntFileName from assetsWithContentURL2 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.highresCntId and c.contentId != 755 ) , assets as ( select a.*, case when right( a.contentURL, charindex( '.', reverse( a.contentURL )) - 1 ) in ( 'BMP','GIF','JPEG','JPG','PNG','TIF','TIFF','WBMP' ) then left( a.contentURL, len( a.contentURL ) - charindex( '.', reverse( a.contentURL ) )) + '_50' + right( a.contentURL, charindex( '.', reverse( a.contentURL )) ) else null end previewURL from assetsWithContentURL3 a ) , categories as ( select 'OPR' contentDomain, categoryId, categoryDescription from tbl_OPR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'CDR' contentDomain, categoryId, categoryDescription from tbl_CDR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'BPL' contentDomain, categoryId, categoryDescription from tbl_BPL_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'DOC' contentDomain, categoryId, categoryName as categoryDescription from tbl_DOC_Categories with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType ) , assetsWithCategory as ( select a.*, c.categoryDescription from assets a left outer join categories c on c.contentDomain = a.contentDomain and c.categoryId = a.categoryId ) ,industryCategories as ( SELECT categoryId, categoryDescription FROM tbl_PRD_RepositoryCategories with (nolock) WHERE containerType = 'SKU' AND ownerID = 0 AND ownerType = '*' AND left(categoryDescription, 3) <> 'GS1' ) , assetsWithIndustryCategory as ( select a.*, ic.categoryDescription as industryCategoryDescription from assetsWithCategory a left outer join industryCategories ic on ic.categoryId = a.categoryId ) , assetsWithPartner as ( select a.*, opr.companyName as partnerName, a.containerId as partnerId, a.containerType as partnerType, opr.territoryId from assetsWithIndustryCategory a inner join tbl_OPR_CLientOperators opr with (nolock) on opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = a.containerId where a.containerType = 'OPR' union all select a.*, cdr.cdr_dstName as partnerName, a.containerId as partnerId, a.containerType as partnerType, cdr.cdr_territoryId as territoryId from assetsWithIndustryCategory a inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = @ownerId and cdr.fsl_tablecode = @ownerType and cdr.cdr_recordId = a.containerId where a.containerType = 'CDR' union all select a.*, mfr.mfr_name as partnerName, mfr.mfr_id as partnerId, 'MFR' as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a inner join tbl_BRO_Principals bpl with (nolock) on bpl.ownerId = @ownerId and bpl.ownerType = @ownerType and bpl.principalId = a.containerId inner join tblManufacturers mfr with (nolock) on mfr.mfr_id = bpl.mfrId where a.containerType = 'BPL' union all select a.*,cast (null as varchar ) as partnerName, cast( null as integer ) as partnerId, cast( null as varchar ) as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a where coalesce( a.containerType, '???') not in ( 'BPL', 'OPR', 'CDR' ) ), assetsWithUser as ( select a.*, m.email updateUserEmail, m.firstName + ' ' + m.lastName as updateUserFullName, case when len(isnull(a.linked_prd,'')) > 0 or len(isnull(a.linked_sku,'')) > 0 then 'Y' else 'N' end as prdLink, case when len(isnull(a.linked_opr,'')) > 0 then 'Y' else 'N' end as oprLink, case when len(isnull(a.linked_cdr,'')) > 0 then 'Y' else 'N' end as cdrLink, case when len(isnull(a.linked_mfr,'')) > 0 then 'Y' else 'N' end as mfrLink from assetsWithPartner as a left outer join tbl_Fspro_members m with (nolock) on m.ownerId > 0 and m.fspro_userId = a.updateUserId ) , assetsWithUser_mappedData as ( select * from assetsWithUser ) , assetsWithUser_preData as ( select * from assetsWithUser_mappedData with (nolock) where ( ( [fileId] = '180' ) and ( ( 1 = 1 ) and ( ( ( [isPublished] = 'Y' ) or ( [isPublished] = 'F' ) ) ) ) ) ) , assetsWithUser_data as ( select * from assetsWithUser_preData ) , assetsWithUser_return as ( select * from assetsWithUser_data where 1 = 1 order by docTitle offset 0 rows fetch next 50 rows only ) , assetsWithUser_result as ( select ( select count(*) from assetsWithUser_data ) as totalCount, ( select * from assetsWithUser_return for json auto, include_null_values ) as data ) select * from assetsWithUser_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER -- 2026-04-16 17:39:48.271 -- 0 {ts '2026-04-16 17:39:49'} SQL declare @ownerId int = 207 ; declare @ownerType varchar(3) = 'MFR' ; declare @contentPrefix varchar(128) = 'http://dev.fsenablers.com/FSLContent/' ; declare @todayDate datetime = ?; with docCategories as ( SELECT cats.attrID as categoryId, cats.attrDescription AS categoryName FROM tbl_ORG_Attributes cats with (nolock) WHERE cats.OwnerID = @ownerId AND cats.FSL_TableCode = @ownerType AND cats.attrTypeID = 31 ), rawAssets as ( select null contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, cast( null as varchar ) docTitle, cast( null as int) as categoryId, cast( null as int ) contentId, cast( null as int) fileId, 0.00 fileSize, null fileExt, null docDescription, cast( null as datetime ) publishDate, cast( null as datetime ) unpublishDate, 'N' isPublished, cast( null as datetime ) uploadDate, cast( null as datetime ) updateDate, cast( null as int ) updateUserId, 0 restricted, -1 thumbnailId, -1 highresCntId, 99 as sortRank , 0 as freshness , 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr, cast( null as varchar ) as assetSelectionKey, 0 as selectedRecord , 'N' as internalOnly, '' as recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds union all select 'DOC' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, d.docTitle, d.repCategoryId as categoryId, d.contentId, d.docId as fileId, CAST((d.fileSize/1024) AS float) as fileSize , d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, case when d.publishDate is not null and d.publishDate > @todayDate then 'F' when d.publishDate is not null and d.unpublishDate is not null and @todayDate >= d.publishDate and @todayDate <= d.unpublishDate then 'Y' when d.publishDate is not null and d.unpublishDate is null and d.publishDate <= @todayDate then 'Y' when d.publishDate is null then 'N' else 'N' end isPublished, d.uploadDate, d.lastUpdatedDate updateDate, d.lastUpdatedBy updateUserId, 0 restricted, thumbnailId, -1 highresCntId, sortRank , coalesce(dateDiff(day, d.lastUpdatedDate, @todayDate),0) as freshness, case when d.privateYesNo = 1 then 'Y' else 'N' end privateYesNo, ou.firstname + ' ' + ou.lastname as uploadedByFullName, d.keyWords, d.unpublishToDo, d.internalNotes , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END), ',') AS linked_opr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END), ',') AS linked_prd , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END), ',') AS linked_sku , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END), ',') AS linked_cdr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END), ',') AS linked_mfr , cast(concat('DOC',':',d.docId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , d.internalOnly, d.recordSource , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 23 THEN li.child_attrID ELSE NULL END), ',') AS permissionGrpIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 7 THEN li.child_attrID ELSE NULL END), ',') AS linked_brokerIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 31 THEN li.child_attrID ELSE NULL END), ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = d.docId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 34 THEN li.child_attrID ELSE NULL END), ',') AS linked_UserMemberIds from tbl_DOC_Documents d with (nolock) LEFT OUTER JOIN tbl_FSPRO_Members ou with (nolock) ON d.AuthorUserId = ou.fspro_userId left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = d.docId and li.child_attrTypeID in (41,59,60,63,42,23,7,31,34) and li.mstr_attrTypeID = 30 left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('DOC',':',d.docId) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType GROUP BY d.docTitle, d.repCategoryId, d.contentId, d.docId,d.fileSize, d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, d.uploadDate, d.lastUpdatedDate, d.lastUpdatedBy,thumbnailId, sortRank , d.privateYesNo , ou.firstname,ou.lastname, d.keyWords, d.unpublishToDo, d.internalNotes,ast.assetSelectionKey, d.internalOnly, d.recordSource union all select 'IMG' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, i.imgTitle as docTitle, i.categoryId, i.contentId, i.imgId as fileId, CAST((i.fileSize/1024) AS float) as fileSize, i.fileExt, i.imgDescription as docDescription, i.publishDate, i.unpublishDate, case when i.publishDate is not null and i.publishDate > @todayDate then 'F' when i.publishDate is not null and i.unpublishDate is not null and @todayDate >= i.publishDate and @todayDate <= i.unpublishDate then 'Y' when i.publishDate is not null and i.unpublishDate is null and i.publishDate <= @todayDate then 'Y' when i.publishDate is null then 'N' else 'N' end isPublished, i.uploadDate, i.lastUpdatedDate updateDate, i.lastUpdatedBy updateUserId, 0 restricted, -1 thumbnailId, i.highresCntId, i.sortRank, coalesce(dateDiff(day, lastUpdatedDate, @todayDate),0) as freshness, case when i.privateYesNo = 1 then 'Y' else 'N' end privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , STRING_AGG(CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END, ',') AS linked_opr , STRING_AGG(CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END, ',') AS linked_prd , STRING_AGG(CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END, ',') AS linked_sku , STRING_AGG(CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END, ',') AS linked_cdr , STRING_AGG(CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END, ',') AS linked_mfr , cast(concat('IMG',':',i.imgId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , i.internalOnly, i.recordSource , STRING_AGG(CASE WHEN c.child_attrTypeID = 23 THEN c.child_attrID ELSE NULL END, ',') AS permissionGrpIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 7 THEN c.child_attrID ELSE NULL END, ',') AS linked_brokerIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 31 THEN c.child_attrID ELSE NULL END, ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = i.imgId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CASE WHEN c.child_attrTypeID = 34 THEN c.child_attrID ELSE NULL END, ',') AS linked_UserMemberIds from tbl_IMG_Images i with (nolock) left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = i.imgId and li.child_attrTypeID in (41,59,60,63,42) and li.mstr_attrTypeID = 33 left outer JOIN tbl_ORG_AttributeLinks c ON c.mstr_attrID = i.imgId and c.child_attrTypeID in (23,7,31,34) and c.mstr_attrTypeID = 30 left outer join tbl_DOC_Categories dc with (nolock) ON c.child_attrTypeID = 31 AND dc.categoryId = c.child_attrID AND dc.ownerId = @ownerId AND dc.fsl_tablecode = @ownerType left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('IMG',':',i.imgId) where i.ownerId = @ownerId and i.fsl_tablecode = @ownerType GROUP BY i.imgTitle, i.CategoryID, i.contentId, i.imgId, i.fileSize, i.fileExt, i.imgDescription, i.publishDate, i.unpublishDate, i.uploadDate, i.lastUpdatedDate, i.lastUpdatedBy, i.highresCntId, i.sortRank, i.privateYesNo,ast.assetSelectionKey, i.internalOnly, i.recordSource ), filteredRawAssets as ( select ra.* from rawAssets ra where (1=1 or ra.linked_brokerIds in (?) ) ) , assetsWithContentURL1 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL, c.fileName as docFileName, lub.firstname + ' ' + lub.lastname as lastUpdatedByFullName from filteredRawAssets ra LEFT OUTER JOIN tbl_FSPRO_Members lub with (nolock) ON ra.updateUserId = lub.fspro_userId inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) , assetsWithContentURL2 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as thumbnailURL, c.fileName as thnFileName from assetsWithContentURL1 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.thumbnailId and c.contentId != 755 ) , assetsWithContentURL3 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as highresURL, c.fileName as highresCntFileName from assetsWithContentURL2 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.highresCntId and c.contentId != 755 ) , assets as ( select a.*, case when right( a.contentURL, charindex( '.', reverse( a.contentURL )) - 1 ) in ( 'BMP','GIF','JPEG','JPG','PNG','TIF','TIFF','WBMP' ) then left( a.contentURL, len( a.contentURL ) - charindex( '.', reverse( a.contentURL ) )) + '_50' + right( a.contentURL, charindex( '.', reverse( a.contentURL )) ) else null end previewURL from assetsWithContentURL3 a ) , categories as ( select 'OPR' contentDomain, categoryId, categoryDescription from tbl_OPR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'CDR' contentDomain, categoryId, categoryDescription from tbl_CDR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'BPL' contentDomain, categoryId, categoryDescription from tbl_BPL_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'DOC' contentDomain, categoryId, categoryName as categoryDescription from tbl_DOC_Categories with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType ) , assetsWithCategory as ( select a.*, c.categoryDescription from assets a left outer join categories c on c.contentDomain = a.contentDomain and c.categoryId = a.categoryId ) ,industryCategories as ( SELECT categoryId, categoryDescription FROM tbl_PRD_RepositoryCategories with (nolock) WHERE containerType = 'SKU' AND ownerID = 0 AND ownerType = '*' AND left(categoryDescription, 3) <> 'GS1' ) , assetsWithIndustryCategory as ( select a.*, ic.categoryDescription as industryCategoryDescription from assetsWithCategory a left outer join industryCategories ic on ic.categoryId = a.categoryId ) , assetsWithPartner as ( select a.*, opr.companyName as partnerName, a.containerId as partnerId, a.containerType as partnerType, opr.territoryId from assetsWithIndustryCategory a inner join tbl_OPR_CLientOperators opr with (nolock) on opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = a.containerId where a.containerType = 'OPR' union all select a.*, cdr.cdr_dstName as partnerName, a.containerId as partnerId, a.containerType as partnerType, cdr.cdr_territoryId as territoryId from assetsWithIndustryCategory a inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = @ownerId and cdr.fsl_tablecode = @ownerType and cdr.cdr_recordId = a.containerId where a.containerType = 'CDR' union all select a.*, mfr.mfr_name as partnerName, mfr.mfr_id as partnerId, 'MFR' as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a inner join tbl_BRO_Principals bpl with (nolock) on bpl.ownerId = @ownerId and bpl.ownerType = @ownerType and bpl.principalId = a.containerId inner join tblManufacturers mfr with (nolock) on mfr.mfr_id = bpl.mfrId where a.containerType = 'BPL' union all select a.*,cast (null as varchar ) as partnerName, cast( null as integer ) as partnerId, cast( null as varchar ) as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a where coalesce( a.containerType, '???') not in ( 'BPL', 'OPR', 'CDR' ) ), assetsWithUser as ( select a.*, m.email updateUserEmail, m.firstName + ' ' + m.lastName as updateUserFullName, case when len(isnull(a.linked_prd,'')) > 0 or len(isnull(a.linked_sku,'')) > 0 then 'Y' else 'N' end as prdLink, case when len(isnull(a.linked_opr,'')) > 0 then 'Y' else 'N' end as oprLink, case when len(isnull(a.linked_cdr,'')) > 0 then 'Y' else 'N' end as cdrLink, case when len(isnull(a.linked_mfr,'')) > 0 then 'Y' else 'N' end as mfrLink from assetsWithPartner as a left outer join tbl_Fspro_members m with (nolock) on m.ownerId > 0 and m.fspro_userId = a.updateUserId ) , assetsWithUser_mappedData as ( select * from assetsWithUser ) , assetsWithUser_preData as ( select * from assetsWithUser_mappedData with (nolock) where ( ( [fileId] = '1809' ) and ( ( 1 = 1 ) and ( ( ( [isPublished] = 'Y' ) or ( [isPublished] = 'F' ) ) ) ) ) ) , assetsWithUser_data as ( select * from assetsWithUser_preData ) , assetsWithUser_return as ( select * from assetsWithUser_data where 1 = 1 order by docTitle offset 0 rows fetch next 50 rows only ) , assetsWithUser_result as ( select ( select count(*) from assetsWithUser_data ) as totalCount, ( select * from assetsWithUser_return for json auto, include_null_values ) as data ) select * from assetsWithUser_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER -- 2026-04-16 17:39:49.363 -- 0 {ts '2026-04-16 17:39:50'} SQL declare @ownerId int = 207 ; declare @ownerType varchar(3) = 'MFR' ; declare @contentPrefix varchar(128) = 'http://dev.fsenablers.com/FSLContent/' ; declare @todayDate datetime = ?; with docCategories as ( SELECT cats.attrID as categoryId, cats.attrDescription AS categoryName FROM tbl_ORG_Attributes cats with (nolock) WHERE cats.OwnerID = @ownerId AND cats.FSL_TableCode = @ownerType AND cats.attrTypeID = 31 ), rawAssets as ( select null contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, cast( null as varchar ) docTitle, cast( null as int) as categoryId, cast( null as int ) contentId, cast( null as int) fileId, 0.00 fileSize, null fileExt, null docDescription, cast( null as datetime ) publishDate, cast( null as datetime ) unpublishDate, 'N' isPublished, cast( null as datetime ) uploadDate, cast( null as datetime ) updateDate, cast( null as int ) updateUserId, 0 restricted, -1 thumbnailId, -1 highresCntId, 99 as sortRank , 0 as freshness , 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr, cast( null as varchar ) as assetSelectionKey, 0 as selectedRecord , 'N' as internalOnly, '' as recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds union all select 'DOC' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, d.docTitle, d.repCategoryId as categoryId, d.contentId, d.docId as fileId, CAST((d.fileSize/1024) AS float) as fileSize , d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, case when d.publishDate is not null and d.publishDate > @todayDate then 'F' when d.publishDate is not null and d.unpublishDate is not null and @todayDate >= d.publishDate and @todayDate <= d.unpublishDate then 'Y' when d.publishDate is not null and d.unpublishDate is null and d.publishDate <= @todayDate then 'Y' when d.publishDate is null then 'N' else 'N' end isPublished, d.uploadDate, d.lastUpdatedDate updateDate, d.lastUpdatedBy updateUserId, 0 restricted, thumbnailId, -1 highresCntId, sortRank , coalesce(dateDiff(day, d.lastUpdatedDate, @todayDate),0) as freshness, case when d.privateYesNo = 1 then 'Y' else 'N' end privateYesNo, ou.firstname + ' ' + ou.lastname as uploadedByFullName, d.keyWords, d.unpublishToDo, d.internalNotes , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END), ',') AS linked_opr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END), ',') AS linked_prd , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END), ',') AS linked_sku , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END), ',') AS linked_cdr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END), ',') AS linked_mfr , cast(concat('DOC',':',d.docId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , d.internalOnly, d.recordSource , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 23 THEN li.child_attrID ELSE NULL END), ',') AS permissionGrpIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 7 THEN li.child_attrID ELSE NULL END), ',') AS linked_brokerIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 31 THEN li.child_attrID ELSE NULL END), ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = d.docId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 34 THEN li.child_attrID ELSE NULL END), ',') AS linked_UserMemberIds from tbl_DOC_Documents d with (nolock) LEFT OUTER JOIN tbl_FSPRO_Members ou with (nolock) ON d.AuthorUserId = ou.fspro_userId left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = d.docId and li.child_attrTypeID in (41,59,60,63,42,23,7,31,34) and li.mstr_attrTypeID = 30 left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('DOC',':',d.docId) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType GROUP BY d.docTitle, d.repCategoryId, d.contentId, d.docId,d.fileSize, d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, d.uploadDate, d.lastUpdatedDate, d.lastUpdatedBy,thumbnailId, sortRank , d.privateYesNo , ou.firstname,ou.lastname, d.keyWords, d.unpublishToDo, d.internalNotes,ast.assetSelectionKey, d.internalOnly, d.recordSource union all select 'IMG' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, i.imgTitle as docTitle, i.categoryId, i.contentId, i.imgId as fileId, CAST((i.fileSize/1024) AS float) as fileSize, i.fileExt, i.imgDescription as docDescription, i.publishDate, i.unpublishDate, case when i.publishDate is not null and i.publishDate > @todayDate then 'F' when i.publishDate is not null and i.unpublishDate is not null and @todayDate >= i.publishDate and @todayDate <= i.unpublishDate then 'Y' when i.publishDate is not null and i.unpublishDate is null and i.publishDate <= @todayDate then 'Y' when i.publishDate is null then 'N' else 'N' end isPublished, i.uploadDate, i.lastUpdatedDate updateDate, i.lastUpdatedBy updateUserId, 0 restricted, -1 thumbnailId, i.highresCntId, i.sortRank, coalesce(dateDiff(day, lastUpdatedDate, @todayDate),0) as freshness, case when i.privateYesNo = 1 then 'Y' else 'N' end privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , STRING_AGG(CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END, ',') AS linked_opr , STRING_AGG(CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END, ',') AS linked_prd , STRING_AGG(CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END, ',') AS linked_sku , STRING_AGG(CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END, ',') AS linked_cdr , STRING_AGG(CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END, ',') AS linked_mfr , cast(concat('IMG',':',i.imgId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , i.internalOnly, i.recordSource , STRING_AGG(CASE WHEN c.child_attrTypeID = 23 THEN c.child_attrID ELSE NULL END, ',') AS permissionGrpIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 7 THEN c.child_attrID ELSE NULL END, ',') AS linked_brokerIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 31 THEN c.child_attrID ELSE NULL END, ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = i.imgId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CASE WHEN c.child_attrTypeID = 34 THEN c.child_attrID ELSE NULL END, ',') AS linked_UserMemberIds from tbl_IMG_Images i with (nolock) left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = i.imgId and li.child_attrTypeID in (41,59,60,63,42) and li.mstr_attrTypeID = 33 left outer JOIN tbl_ORG_AttributeLinks c ON c.mstr_attrID = i.imgId and c.child_attrTypeID in (23,7,31,34) and c.mstr_attrTypeID = 30 left outer join tbl_DOC_Categories dc with (nolock) ON c.child_attrTypeID = 31 AND dc.categoryId = c.child_attrID AND dc.ownerId = @ownerId AND dc.fsl_tablecode = @ownerType left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('IMG',':',i.imgId) where i.ownerId = @ownerId and i.fsl_tablecode = @ownerType GROUP BY i.imgTitle, i.CategoryID, i.contentId, i.imgId, i.fileSize, i.fileExt, i.imgDescription, i.publishDate, i.unpublishDate, i.uploadDate, i.lastUpdatedDate, i.lastUpdatedBy, i.highresCntId, i.sortRank, i.privateYesNo,ast.assetSelectionKey, i.internalOnly, i.recordSource ), filteredRawAssets as ( select ra.* from rawAssets ra where (1=1 or ra.linked_brokerIds in (?) ) ) , assetsWithContentURL1 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL, c.fileName as docFileName, lub.firstname + ' ' + lub.lastname as lastUpdatedByFullName from filteredRawAssets ra LEFT OUTER JOIN tbl_FSPRO_Members lub with (nolock) ON ra.updateUserId = lub.fspro_userId inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) , assetsWithContentURL2 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as thumbnailURL, c.fileName as thnFileName from assetsWithContentURL1 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.thumbnailId and c.contentId != 755 ) , assetsWithContentURL3 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as highresURL, c.fileName as highresCntFileName from assetsWithContentURL2 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.highresCntId and c.contentId != 755 ) , assets as ( select a.*, case when right( a.contentURL, charindex( '.', reverse( a.contentURL )) - 1 ) in ( 'BMP','GIF','JPEG','JPG','PNG','TIF','TIFF','WBMP' ) then left( a.contentURL, len( a.contentURL ) - charindex( '.', reverse( a.contentURL ) )) + '_50' + right( a.contentURL, charindex( '.', reverse( a.contentURL )) ) else null end previewURL from assetsWithContentURL3 a ) , categories as ( select 'OPR' contentDomain, categoryId, categoryDescription from tbl_OPR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'CDR' contentDomain, categoryId, categoryDescription from tbl_CDR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'BPL' contentDomain, categoryId, categoryDescription from tbl_BPL_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'DOC' contentDomain, categoryId, categoryName as categoryDescription from tbl_DOC_Categories with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType ) , assetsWithCategory as ( select a.*, c.categoryDescription from assets a left outer join categories c on c.contentDomain = a.contentDomain and c.categoryId = a.categoryId ) ,industryCategories as ( SELECT categoryId, categoryDescription FROM tbl_PRD_RepositoryCategories with (nolock) WHERE containerType = 'SKU' AND ownerID = 0 AND ownerType = '*' AND left(categoryDescription, 3) <> 'GS1' ) , assetsWithIndustryCategory as ( select a.*, ic.categoryDescription as industryCategoryDescription from assetsWithCategory a left outer join industryCategories ic on ic.categoryId = a.categoryId ) , assetsWithPartner as ( select a.*, opr.companyName as partnerName, a.containerId as partnerId, a.containerType as partnerType, opr.territoryId from assetsWithIndustryCategory a inner join tbl_OPR_CLientOperators opr with (nolock) on opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = a.containerId where a.containerType = 'OPR' union all select a.*, cdr.cdr_dstName as partnerName, a.containerId as partnerId, a.containerType as partnerType, cdr.cdr_territoryId as territoryId from assetsWithIndustryCategory a inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = @ownerId and cdr.fsl_tablecode = @ownerType and cdr.cdr_recordId = a.containerId where a.containerType = 'CDR' union all select a.*, mfr.mfr_name as partnerName, mfr.mfr_id as partnerId, 'MFR' as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a inner join tbl_BRO_Principals bpl with (nolock) on bpl.ownerId = @ownerId and bpl.ownerType = @ownerType and bpl.principalId = a.containerId inner join tblManufacturers mfr with (nolock) on mfr.mfr_id = bpl.mfrId where a.containerType = 'BPL' union all select a.*,cast (null as varchar ) as partnerName, cast( null as integer ) as partnerId, cast( null as varchar ) as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a where coalesce( a.containerType, '???') not in ( 'BPL', 'OPR', 'CDR' ) ), assetsWithUser as ( select a.*, m.email updateUserEmail, m.firstName + ' ' + m.lastName as updateUserFullName, case when len(isnull(a.linked_prd,'')) > 0 or len(isnull(a.linked_sku,'')) > 0 then 'Y' else 'N' end as prdLink, case when len(isnull(a.linked_opr,'')) > 0 then 'Y' else 'N' end as oprLink, case when len(isnull(a.linked_cdr,'')) > 0 then 'Y' else 'N' end as cdrLink, case when len(isnull(a.linked_mfr,'')) > 0 then 'Y' else 'N' end as mfrLink from assetsWithPartner as a left outer join tbl_Fspro_members m with (nolock) on m.ownerId > 0 and m.fspro_userId = a.updateUserId ) , assetsWithUser_mappedData as ( select * from assetsWithUser ) , assetsWithUser_preData as ( select * from assetsWithUser_mappedData with (nolock) where ( ( [fileId] = '180964' ) and ( ( 1 = 1 ) and ( ( ( [isPublished] = 'Y' ) or ( [isPublished] = 'F' ) ) ) ) ) ) , assetsWithUser_data as ( select * from assetsWithUser_preData ) , assetsWithUser_return as ( select * from assetsWithUser_data where 1 = 1 order by docTitle offset 0 rows fetch next 50 rows only ) , assetsWithUser_result as ( select ( select count(*) from assetsWithUser_data ) as totalCount, ( select * from assetsWithUser_return for json auto, include_null_values ) as data ) select * from assetsWithUser_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER -- 2026-04-16 17:39:50.629 -- 0 {ts '2026-04-16 17:39:54'} SQL declare @ownerId int = 207 ; declare @ownerType varchar(3) = 'MFR' ; declare @contentPrefix varchar(128) = 'http://dev.fsenablers.com/FSLContent/' ; declare @todayDate datetime = ?; with docCategories as ( SELECT cats.attrID as categoryId, cats.attrDescription AS categoryName FROM tbl_ORG_Attributes cats with (nolock) WHERE cats.OwnerID = @ownerId AND cats.FSL_TableCode = @ownerType AND cats.attrTypeID = 31 ), rawAssets as ( select null contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, cast( null as varchar ) docTitle, cast( null as int) as categoryId, cast( null as int ) contentId, cast( null as int) fileId, 0.00 fileSize, null fileExt, null docDescription, cast( null as datetime ) publishDate, cast( null as datetime ) unpublishDate, 'N' isPublished, cast( null as datetime ) uploadDate, cast( null as datetime ) updateDate, cast( null as int ) updateUserId, 0 restricted, -1 thumbnailId, -1 highresCntId, 99 as sortRank , 0 as freshness , 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr, cast( null as varchar ) as assetSelectionKey, 0 as selectedRecord , 'N' as internalOnly, '' as recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds union all select 'DOC' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, d.docTitle, d.repCategoryId as categoryId, d.contentId, d.docId as fileId, CAST((d.fileSize/1024) AS float) as fileSize , d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, case when d.publishDate is not null and d.publishDate > @todayDate then 'F' when d.publishDate is not null and d.unpublishDate is not null and @todayDate >= d.publishDate and @todayDate <= d.unpublishDate then 'Y' when d.publishDate is not null and d.unpublishDate is null and d.publishDate <= @todayDate then 'Y' when d.publishDate is null then 'N' else 'N' end isPublished, d.uploadDate, d.lastUpdatedDate updateDate, d.lastUpdatedBy updateUserId, 0 restricted, thumbnailId, -1 highresCntId, sortRank , coalesce(dateDiff(day, d.lastUpdatedDate, @todayDate),0) as freshness, case when d.privateYesNo = 1 then 'Y' else 'N' end privateYesNo, ou.firstname + ' ' + ou.lastname as uploadedByFullName, d.keyWords, d.unpublishToDo, d.internalNotes , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END), ',') AS linked_opr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END), ',') AS linked_prd , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END), ',') AS linked_sku , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END), ',') AS linked_cdr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END), ',') AS linked_mfr , cast(concat('DOC',':',d.docId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , d.internalOnly, d.recordSource , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 23 THEN li.child_attrID ELSE NULL END), ',') AS permissionGrpIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 7 THEN li.child_attrID ELSE NULL END), ',') AS linked_brokerIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 31 THEN li.child_attrID ELSE NULL END), ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = d.docId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 34 THEN li.child_attrID ELSE NULL END), ',') AS linked_UserMemberIds from tbl_DOC_Documents d with (nolock) LEFT OUTER JOIN tbl_FSPRO_Members ou with (nolock) ON d.AuthorUserId = ou.fspro_userId left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = d.docId and li.child_attrTypeID in (41,59,60,63,42,23,7,31,34) and li.mstr_attrTypeID = 30 left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('DOC',':',d.docId) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType GROUP BY d.docTitle, d.repCategoryId, d.contentId, d.docId,d.fileSize, d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, d.uploadDate, d.lastUpdatedDate, d.lastUpdatedBy,thumbnailId, sortRank , d.privateYesNo , ou.firstname,ou.lastname, d.keyWords, d.unpublishToDo, d.internalNotes,ast.assetSelectionKey, d.internalOnly, d.recordSource union all select 'IMG' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, i.imgTitle as docTitle, i.categoryId, i.contentId, i.imgId as fileId, CAST((i.fileSize/1024) AS float) as fileSize, i.fileExt, i.imgDescription as docDescription, i.publishDate, i.unpublishDate, case when i.publishDate is not null and i.publishDate > @todayDate then 'F' when i.publishDate is not null and i.unpublishDate is not null and @todayDate >= i.publishDate and @todayDate <= i.unpublishDate then 'Y' when i.publishDate is not null and i.unpublishDate is null and i.publishDate <= @todayDate then 'Y' when i.publishDate is null then 'N' else 'N' end isPublished, i.uploadDate, i.lastUpdatedDate updateDate, i.lastUpdatedBy updateUserId, 0 restricted, -1 thumbnailId, i.highresCntId, i.sortRank, coalesce(dateDiff(day, lastUpdatedDate, @todayDate),0) as freshness, case when i.privateYesNo = 1 then 'Y' else 'N' end privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , STRING_AGG(CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END, ',') AS linked_opr , STRING_AGG(CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END, ',') AS linked_prd , STRING_AGG(CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END, ',') AS linked_sku , STRING_AGG(CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END, ',') AS linked_cdr , STRING_AGG(CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END, ',') AS linked_mfr , cast(concat('IMG',':',i.imgId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , i.internalOnly, i.recordSource , STRING_AGG(CASE WHEN c.child_attrTypeID = 23 THEN c.child_attrID ELSE NULL END, ',') AS permissionGrpIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 7 THEN c.child_attrID ELSE NULL END, ',') AS linked_brokerIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 31 THEN c.child_attrID ELSE NULL END, ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = i.imgId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CASE WHEN c.child_attrTypeID = 34 THEN c.child_attrID ELSE NULL END, ',') AS linked_UserMemberIds from tbl_IMG_Images i with (nolock) left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = i.imgId and li.child_attrTypeID in (41,59,60,63,42) and li.mstr_attrTypeID = 33 left outer JOIN tbl_ORG_AttributeLinks c ON c.mstr_attrID = i.imgId and c.child_attrTypeID in (23,7,31,34) and c.mstr_attrTypeID = 30 left outer join tbl_DOC_Categories dc with (nolock) ON c.child_attrTypeID = 31 AND dc.categoryId = c.child_attrID AND dc.ownerId = @ownerId AND dc.fsl_tablecode = @ownerType left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('IMG',':',i.imgId) where i.ownerId = @ownerId and i.fsl_tablecode = @ownerType GROUP BY i.imgTitle, i.CategoryID, i.contentId, i.imgId, i.fileSize, i.fileExt, i.imgDescription, i.publishDate, i.unpublishDate, i.uploadDate, i.lastUpdatedDate, i.lastUpdatedBy, i.highresCntId, i.sortRank, i.privateYesNo,ast.assetSelectionKey, i.internalOnly, i.recordSource ), filteredRawAssets as ( select ra.* from rawAssets ra where (1=1 or ra.linked_brokerIds in (?) ) ) , assetsWithContentURL1 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL, c.fileName as docFileName, lub.firstname + ' ' + lub.lastname as lastUpdatedByFullName from filteredRawAssets ra LEFT OUTER JOIN tbl_FSPRO_Members lub with (nolock) ON ra.updateUserId = lub.fspro_userId inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) , assetsWithContentURL2 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as thumbnailURL, c.fileName as thnFileName from assetsWithContentURL1 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.thumbnailId and c.contentId != 755 ) , assetsWithContentURL3 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as highresURL, c.fileName as highresCntFileName from assetsWithContentURL2 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.highresCntId and c.contentId != 755 ) , assets as ( select a.*, case when right( a.contentURL, charindex( '.', reverse( a.contentURL )) - 1 ) in ( 'BMP','GIF','JPEG','JPG','PNG','TIF','TIFF','WBMP' ) then left( a.contentURL, len( a.contentURL ) - charindex( '.', reverse( a.contentURL ) )) + '_50' + right( a.contentURL, charindex( '.', reverse( a.contentURL )) ) else null end previewURL from assetsWithContentURL3 a ) , categories as ( select 'OPR' contentDomain, categoryId, categoryDescription from tbl_OPR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'CDR' contentDomain, categoryId, categoryDescription from tbl_CDR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'BPL' contentDomain, categoryId, categoryDescription from tbl_BPL_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'DOC' contentDomain, categoryId, categoryName as categoryDescription from tbl_DOC_Categories with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType ) , assetsWithCategory as ( select a.*, c.categoryDescription from assets a left outer join categories c on c.contentDomain = a.contentDomain and c.categoryId = a.categoryId ) ,industryCategories as ( SELECT categoryId, categoryDescription FROM tbl_PRD_RepositoryCategories with (nolock) WHERE containerType = 'SKU' AND ownerID = 0 AND ownerType = '*' AND left(categoryDescription, 3) <> 'GS1' ) , assetsWithIndustryCategory as ( select a.*, ic.categoryDescription as industryCategoryDescription from assetsWithCategory a left outer join industryCategories ic on ic.categoryId = a.categoryId ) , assetsWithPartner as ( select a.*, opr.companyName as partnerName, a.containerId as partnerId, a.containerType as partnerType, opr.territoryId from assetsWithIndustryCategory a inner join tbl_OPR_CLientOperators opr with (nolock) on opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = a.containerId where a.containerType = 'OPR' union all select a.*, cdr.cdr_dstName as partnerName, a.containerId as partnerId, a.containerType as partnerType, cdr.cdr_territoryId as territoryId from assetsWithIndustryCategory a inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = @ownerId and cdr.fsl_tablecode = @ownerType and cdr.cdr_recordId = a.containerId where a.containerType = 'CDR' union all select a.*, mfr.mfr_name as partnerName, mfr.mfr_id as partnerId, 'MFR' as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a inner join tbl_BRO_Principals bpl with (nolock) on bpl.ownerId = @ownerId and bpl.ownerType = @ownerType and bpl.principalId = a.containerId inner join tblManufacturers mfr with (nolock) on mfr.mfr_id = bpl.mfrId where a.containerType = 'BPL' union all select a.*,cast (null as varchar ) as partnerName, cast( null as integer ) as partnerId, cast( null as varchar ) as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a where coalesce( a.containerType, '???') not in ( 'BPL', 'OPR', 'CDR' ) ), assetsWithUser as ( select a.*, m.email updateUserEmail, m.firstName + ' ' + m.lastName as updateUserFullName, case when len(isnull(a.linked_prd,'')) > 0 or len(isnull(a.linked_sku,'')) > 0 then 'Y' else 'N' end as prdLink, case when len(isnull(a.linked_opr,'')) > 0 then 'Y' else 'N' end as oprLink, case when len(isnull(a.linked_cdr,'')) > 0 then 'Y' else 'N' end as cdrLink, case when len(isnull(a.linked_mfr,'')) > 0 then 'Y' else 'N' end as mfrLink from assetsWithPartner as a left outer join tbl_Fspro_members m with (nolock) on m.ownerId > 0 and m.fspro_userId = a.updateUserId ) , assetsWithUser_mappedData as ( select * from assetsWithUser ) , assetsWithUser_preData as ( select * from assetsWithUser_mappedData with (nolock) where ( ( 1 = 1 ) and ( ( ( [isPublished] = 'Y' ) or ( [isPublished] = 'F' ) ) ) ) ) , assetsWithUser_data as ( select * from assetsWithUser_preData ) , assetsWithUser_return as ( select * from assetsWithUser_data where 1 = 1 order by docTitle offset 0 rows fetch next 50 rows only ) , assetsWithUser_result as ( select ( select count(*) from assetsWithUser_data ) as totalCount, ( select * from assetsWithUser_return for json auto, include_null_values ) as data ) select * from assetsWithUser_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER -- 2026-04-16 17:39:53.99 -- 0 {ts '2026-04-16 17:40:05'} SQL declare @ownerId int = 207 ; declare @ownerType varchar(3) = 'MFR' ; declare @contentPrefix varchar(128) = 'http://dev.fsenablers.com/FSLContent/' ; declare @todayDate datetime = ?; with docCategories as ( SELECT cats.attrID as categoryId, cats.attrDescription AS categoryName FROM tbl_ORG_Attributes cats with (nolock) WHERE cats.OwnerID = @ownerId AND cats.FSL_TableCode = @ownerType AND cats.attrTypeID = 31 ), rawAssets as ( select null contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, cast( null as varchar ) docTitle, cast( null as int) as categoryId, cast( null as int ) contentId, cast( null as int) fileId, 0.00 fileSize, null fileExt, null docDescription, cast( null as datetime ) publishDate, cast( null as datetime ) unpublishDate, 'N' isPublished, cast( null as datetime ) uploadDate, cast( null as datetime ) updateDate, cast( null as int ) updateUserId, 0 restricted, -1 thumbnailId, -1 highresCntId, 99 as sortRank , 0 as freshness , 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr, cast( null as varchar ) as assetSelectionKey, 0 as selectedRecord , 'N' as internalOnly, '' as recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds union all select 'DOC' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, d.docTitle, d.repCategoryId as categoryId, d.contentId, d.docId as fileId, CAST((d.fileSize/1024) AS float) as fileSize , d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, case when d.publishDate is not null and d.publishDate > @todayDate then 'F' when d.publishDate is not null and d.unpublishDate is not null and @todayDate >= d.publishDate and @todayDate <= d.unpublishDate then 'Y' when d.publishDate is not null and d.unpublishDate is null and d.publishDate <= @todayDate then 'Y' when d.publishDate is null then 'N' else 'N' end isPublished, d.uploadDate, d.lastUpdatedDate updateDate, d.lastUpdatedBy updateUserId, 0 restricted, thumbnailId, -1 highresCntId, sortRank , coalesce(dateDiff(day, d.lastUpdatedDate, @todayDate),0) as freshness, case when d.privateYesNo = 1 then 'Y' else 'N' end privateYesNo, ou.firstname + ' ' + ou.lastname as uploadedByFullName, d.keyWords, d.unpublishToDo, d.internalNotes , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END), ',') AS linked_opr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END), ',') AS linked_prd , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END), ',') AS linked_sku , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END), ',') AS linked_cdr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END), ',') AS linked_mfr , cast(concat('DOC',':',d.docId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , d.internalOnly, d.recordSource , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 23 THEN li.child_attrID ELSE NULL END), ',') AS permissionGrpIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 7 THEN li.child_attrID ELSE NULL END), ',') AS linked_brokerIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 31 THEN li.child_attrID ELSE NULL END), ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = d.docId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 34 THEN li.child_attrID ELSE NULL END), ',') AS linked_UserMemberIds from tbl_DOC_Documents d with (nolock) LEFT OUTER JOIN tbl_FSPRO_Members ou with (nolock) ON d.AuthorUserId = ou.fspro_userId left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = d.docId and li.child_attrTypeID in (41,59,60,63,42,23,7,31,34) and li.mstr_attrTypeID = 30 left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('DOC',':',d.docId) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType GROUP BY d.docTitle, d.repCategoryId, d.contentId, d.docId,d.fileSize, d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, d.uploadDate, d.lastUpdatedDate, d.lastUpdatedBy,thumbnailId, sortRank , d.privateYesNo , ou.firstname,ou.lastname, d.keyWords, d.unpublishToDo, d.internalNotes,ast.assetSelectionKey, d.internalOnly, d.recordSource union all select 'IMG' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, i.imgTitle as docTitle, i.categoryId, i.contentId, i.imgId as fileId, CAST((i.fileSize/1024) AS float) as fileSize, i.fileExt, i.imgDescription as docDescription, i.publishDate, i.unpublishDate, case when i.publishDate is not null and i.publishDate > @todayDate then 'F' when i.publishDate is not null and i.unpublishDate is not null and @todayDate >= i.publishDate and @todayDate <= i.unpublishDate then 'Y' when i.publishDate is not null and i.unpublishDate is null and i.publishDate <= @todayDate then 'Y' when i.publishDate is null then 'N' else 'N' end isPublished, i.uploadDate, i.lastUpdatedDate updateDate, i.lastUpdatedBy updateUserId, 0 restricted, -1 thumbnailId, i.highresCntId, i.sortRank, coalesce(dateDiff(day, lastUpdatedDate, @todayDate),0) as freshness, case when i.privateYesNo = 1 then 'Y' else 'N' end privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , STRING_AGG(CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END, ',') AS linked_opr , STRING_AGG(CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END, ',') AS linked_prd , STRING_AGG(CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END, ',') AS linked_sku , STRING_AGG(CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END, ',') AS linked_cdr , STRING_AGG(CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END, ',') AS linked_mfr , cast(concat('IMG',':',i.imgId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , i.internalOnly, i.recordSource , STRING_AGG(CASE WHEN c.child_attrTypeID = 23 THEN c.child_attrID ELSE NULL END, ',') AS permissionGrpIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 7 THEN c.child_attrID ELSE NULL END, ',') AS linked_brokerIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 31 THEN c.child_attrID ELSE NULL END, ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = i.imgId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CASE WHEN c.child_attrTypeID = 34 THEN c.child_attrID ELSE NULL END, ',') AS linked_UserMemberIds from tbl_IMG_Images i with (nolock) left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = i.imgId and li.child_attrTypeID in (41,59,60,63,42) and li.mstr_attrTypeID = 33 left outer JOIN tbl_ORG_AttributeLinks c ON c.mstr_attrID = i.imgId and c.child_attrTypeID in (23,7,31,34) and c.mstr_attrTypeID = 30 left outer join tbl_DOC_Categories dc with (nolock) ON c.child_attrTypeID = 31 AND dc.categoryId = c.child_attrID AND dc.ownerId = @ownerId AND dc.fsl_tablecode = @ownerType left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('IMG',':',i.imgId) where i.ownerId = @ownerId and i.fsl_tablecode = @ownerType GROUP BY i.imgTitle, i.CategoryID, i.contentId, i.imgId, i.fileSize, i.fileExt, i.imgDescription, i.publishDate, i.unpublishDate, i.uploadDate, i.lastUpdatedDate, i.lastUpdatedBy, i.highresCntId, i.sortRank, i.privateYesNo,ast.assetSelectionKey, i.internalOnly, i.recordSource ), filteredRawAssets as ( select ra.* from rawAssets ra where (1=1 or ra.linked_brokerIds in (?) ) ) , assetsWithContentURL1 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL, c.fileName as docFileName, lub.firstname + ' ' + lub.lastname as lastUpdatedByFullName from filteredRawAssets ra LEFT OUTER JOIN tbl_FSPRO_Members lub with (nolock) ON ra.updateUserId = lub.fspro_userId inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) , assetsWithContentURL2 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as thumbnailURL, c.fileName as thnFileName from assetsWithContentURL1 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.thumbnailId and c.contentId != 755 ) , assetsWithContentURL3 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as highresURL, c.fileName as highresCntFileName from assetsWithContentURL2 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.highresCntId and c.contentId != 755 ) , assets as ( select a.*, case when right( a.contentURL, charindex( '.', reverse( a.contentURL )) - 1 ) in ( 'BMP','GIF','JPEG','JPG','PNG','TIF','TIFF','WBMP' ) then left( a.contentURL, len( a.contentURL ) - charindex( '.', reverse( a.contentURL ) )) + '_50' + right( a.contentURL, charindex( '.', reverse( a.contentURL )) ) else null end previewURL from assetsWithContentURL3 a ) , categories as ( select 'OPR' contentDomain, categoryId, categoryDescription from tbl_OPR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'CDR' contentDomain, categoryId, categoryDescription from tbl_CDR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'BPL' contentDomain, categoryId, categoryDescription from tbl_BPL_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'DOC' contentDomain, categoryId, categoryName as categoryDescription from tbl_DOC_Categories with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType ) , assetsWithCategory as ( select a.*, c.categoryDescription from assets a left outer join categories c on c.contentDomain = a.contentDomain and c.categoryId = a.categoryId ) ,industryCategories as ( SELECT categoryId, categoryDescription FROM tbl_PRD_RepositoryCategories with (nolock) WHERE containerType = 'SKU' AND ownerID = 0 AND ownerType = '*' AND left(categoryDescription, 3) <> 'GS1' ) , assetsWithIndustryCategory as ( select a.*, ic.categoryDescription as industryCategoryDescription from assetsWithCategory a left outer join industryCategories ic on ic.categoryId = a.categoryId ) , assetsWithPartner as ( select a.*, opr.companyName as partnerName, a.containerId as partnerId, a.containerType as partnerType, opr.territoryId from assetsWithIndustryCategory a inner join tbl_OPR_CLientOperators opr with (nolock) on opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = a.containerId where a.containerType = 'OPR' union all select a.*, cdr.cdr_dstName as partnerName, a.containerId as partnerId, a.containerType as partnerType, cdr.cdr_territoryId as territoryId from assetsWithIndustryCategory a inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = @ownerId and cdr.fsl_tablecode = @ownerType and cdr.cdr_recordId = a.containerId where a.containerType = 'CDR' union all select a.*, mfr.mfr_name as partnerName, mfr.mfr_id as partnerId, 'MFR' as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a inner join tbl_BRO_Principals bpl with (nolock) on bpl.ownerId = @ownerId and bpl.ownerType = @ownerType and bpl.principalId = a.containerId inner join tblManufacturers mfr with (nolock) on mfr.mfr_id = bpl.mfrId where a.containerType = 'BPL' union all select a.*,cast (null as varchar ) as partnerName, cast( null as integer ) as partnerId, cast( null as varchar ) as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a where coalesce( a.containerType, '???') not in ( 'BPL', 'OPR', 'CDR' ) ), assetsWithUser as ( select a.*, m.email updateUserEmail, m.firstName + ' ' + m.lastName as updateUserFullName, case when len(isnull(a.linked_prd,'')) > 0 or len(isnull(a.linked_sku,'')) > 0 then 'Y' else 'N' end as prdLink, case when len(isnull(a.linked_opr,'')) > 0 then 'Y' else 'N' end as oprLink, case when len(isnull(a.linked_cdr,'')) > 0 then 'Y' else 'N' end as cdrLink, case when len(isnull(a.linked_mfr,'')) > 0 then 'Y' else 'N' end as mfrLink from assetsWithPartner as a left outer join tbl_Fspro_members m with (nolock) on m.ownerId > 0 and m.fspro_userId = a.updateUserId ) , assetsWithUser_mappedData as ( select * from assetsWithUser ) , assetsWithUser_preData as ( select * from assetsWithUser_mappedData with (nolock) where ( ( [fileId] = '180' ) and ( ( 1 = 1 ) and ( ( ( [isPublished] = 'Y' ) or ( [isPublished] = 'F' ) ) ) ) ) ) , assetsWithUser_data as ( select * from assetsWithUser_preData ) , assetsWithUser_return as ( select * from assetsWithUser_data where 1 = 1 order by docTitle offset 0 rows fetch next 50 rows only ) , assetsWithUser_result as ( select ( select count(*) from assetsWithUser_data ) as totalCount, ( select * from assetsWithUser_return for json auto, include_null_values ) as data ) select * from assetsWithUser_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER -- 2026-04-16 17:40:05.19 -- 0 {ts '2026-04-16 17:40:07'} SQL declare @ownerId int = 207 ; declare @ownerType varchar(3) = 'MFR' ; declare @contentPrefix varchar(128) = 'http://dev.fsenablers.com/FSLContent/' ; declare @todayDate datetime = ?; with docCategories as ( SELECT cats.attrID as categoryId, cats.attrDescription AS categoryName FROM tbl_ORG_Attributes cats with (nolock) WHERE cats.OwnerID = @ownerId AND cats.FSL_TableCode = @ownerType AND cats.attrTypeID = 31 ), rawAssets as ( select null contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, cast( null as varchar ) docTitle, cast( null as int) as categoryId, cast( null as int ) contentId, cast( null as int) fileId, 0.00 fileSize, null fileExt, null docDescription, cast( null as datetime ) publishDate, cast( null as datetime ) unpublishDate, 'N' isPublished, cast( null as datetime ) uploadDate, cast( null as datetime ) updateDate, cast( null as int ) updateUserId, 0 restricted, -1 thumbnailId, -1 highresCntId, 99 as sortRank , 0 as freshness , 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr, cast( null as varchar ) as assetSelectionKey, 0 as selectedRecord , 'N' as internalOnly, '' as recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds union all select 'DOC' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, d.docTitle, d.repCategoryId as categoryId, d.contentId, d.docId as fileId, CAST((d.fileSize/1024) AS float) as fileSize , d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, case when d.publishDate is not null and d.publishDate > @todayDate then 'F' when d.publishDate is not null and d.unpublishDate is not null and @todayDate >= d.publishDate and @todayDate <= d.unpublishDate then 'Y' when d.publishDate is not null and d.unpublishDate is null and d.publishDate <= @todayDate then 'Y' when d.publishDate is null then 'N' else 'N' end isPublished, d.uploadDate, d.lastUpdatedDate updateDate, d.lastUpdatedBy updateUserId, 0 restricted, thumbnailId, -1 highresCntId, sortRank , coalesce(dateDiff(day, d.lastUpdatedDate, @todayDate),0) as freshness, case when d.privateYesNo = 1 then 'Y' else 'N' end privateYesNo, ou.firstname + ' ' + ou.lastname as uploadedByFullName, d.keyWords, d.unpublishToDo, d.internalNotes , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END), ',') AS linked_opr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END), ',') AS linked_prd , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END), ',') AS linked_sku , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END), ',') AS linked_cdr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END), ',') AS linked_mfr , cast(concat('DOC',':',d.docId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , d.internalOnly, d.recordSource , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 23 THEN li.child_attrID ELSE NULL END), ',') AS permissionGrpIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 7 THEN li.child_attrID ELSE NULL END), ',') AS linked_brokerIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 31 THEN li.child_attrID ELSE NULL END), ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = d.docId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 34 THEN li.child_attrID ELSE NULL END), ',') AS linked_UserMemberIds from tbl_DOC_Documents d with (nolock) LEFT OUTER JOIN tbl_FSPRO_Members ou with (nolock) ON d.AuthorUserId = ou.fspro_userId left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = d.docId and li.child_attrTypeID in (41,59,60,63,42,23,7,31,34) and li.mstr_attrTypeID = 30 left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('DOC',':',d.docId) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType GROUP BY d.docTitle, d.repCategoryId, d.contentId, d.docId,d.fileSize, d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, d.uploadDate, d.lastUpdatedDate, d.lastUpdatedBy,thumbnailId, sortRank , d.privateYesNo , ou.firstname,ou.lastname, d.keyWords, d.unpublishToDo, d.internalNotes,ast.assetSelectionKey, d.internalOnly, d.recordSource union all select 'IMG' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, i.imgTitle as docTitle, i.categoryId, i.contentId, i.imgId as fileId, CAST((i.fileSize/1024) AS float) as fileSize, i.fileExt, i.imgDescription as docDescription, i.publishDate, i.unpublishDate, case when i.publishDate is not null and i.publishDate > @todayDate then 'F' when i.publishDate is not null and i.unpublishDate is not null and @todayDate >= i.publishDate and @todayDate <= i.unpublishDate then 'Y' when i.publishDate is not null and i.unpublishDate is null and i.publishDate <= @todayDate then 'Y' when i.publishDate is null then 'N' else 'N' end isPublished, i.uploadDate, i.lastUpdatedDate updateDate, i.lastUpdatedBy updateUserId, 0 restricted, -1 thumbnailId, i.highresCntId, i.sortRank, coalesce(dateDiff(day, lastUpdatedDate, @todayDate),0) as freshness, case when i.privateYesNo = 1 then 'Y' else 'N' end privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , STRING_AGG(CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END, ',') AS linked_opr , STRING_AGG(CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END, ',') AS linked_prd , STRING_AGG(CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END, ',') AS linked_sku , STRING_AGG(CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END, ',') AS linked_cdr , STRING_AGG(CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END, ',') AS linked_mfr , cast(concat('IMG',':',i.imgId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , i.internalOnly, i.recordSource , STRING_AGG(CASE WHEN c.child_attrTypeID = 23 THEN c.child_attrID ELSE NULL END, ',') AS permissionGrpIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 7 THEN c.child_attrID ELSE NULL END, ',') AS linked_brokerIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 31 THEN c.child_attrID ELSE NULL END, ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = i.imgId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CASE WHEN c.child_attrTypeID = 34 THEN c.child_attrID ELSE NULL END, ',') AS linked_UserMemberIds from tbl_IMG_Images i with (nolock) left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = i.imgId and li.child_attrTypeID in (41,59,60,63,42) and li.mstr_attrTypeID = 33 left outer JOIN tbl_ORG_AttributeLinks c ON c.mstr_attrID = i.imgId and c.child_attrTypeID in (23,7,31,34) and c.mstr_attrTypeID = 30 left outer join tbl_DOC_Categories dc with (nolock) ON c.child_attrTypeID = 31 AND dc.categoryId = c.child_attrID AND dc.ownerId = @ownerId AND dc.fsl_tablecode = @ownerType left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('IMG',':',i.imgId) where i.ownerId = @ownerId and i.fsl_tablecode = @ownerType GROUP BY i.imgTitle, i.CategoryID, i.contentId, i.imgId, i.fileSize, i.fileExt, i.imgDescription, i.publishDate, i.unpublishDate, i.uploadDate, i.lastUpdatedDate, i.lastUpdatedBy, i.highresCntId, i.sortRank, i.privateYesNo,ast.assetSelectionKey, i.internalOnly, i.recordSource ), filteredRawAssets as ( select ra.* from rawAssets ra where (1=1 or ra.linked_brokerIds in (?) ) ) , assetsWithContentURL1 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL, c.fileName as docFileName, lub.firstname + ' ' + lub.lastname as lastUpdatedByFullName from filteredRawAssets ra LEFT OUTER JOIN tbl_FSPRO_Members lub with (nolock) ON ra.updateUserId = lub.fspro_userId inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) , assetsWithContentURL2 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as thumbnailURL, c.fileName as thnFileName from assetsWithContentURL1 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.thumbnailId and c.contentId != 755 ) , assetsWithContentURL3 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as highresURL, c.fileName as highresCntFileName from assetsWithContentURL2 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.highresCntId and c.contentId != 755 ) , assets as ( select a.*, case when right( a.contentURL, charindex( '.', reverse( a.contentURL )) - 1 ) in ( 'BMP','GIF','JPEG','JPG','PNG','TIF','TIFF','WBMP' ) then left( a.contentURL, len( a.contentURL ) - charindex( '.', reverse( a.contentURL ) )) + '_50' + right( a.contentURL, charindex( '.', reverse( a.contentURL )) ) else null end previewURL from assetsWithContentURL3 a ) , categories as ( select 'OPR' contentDomain, categoryId, categoryDescription from tbl_OPR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'CDR' contentDomain, categoryId, categoryDescription from tbl_CDR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'BPL' contentDomain, categoryId, categoryDescription from tbl_BPL_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'DOC' contentDomain, categoryId, categoryName as categoryDescription from tbl_DOC_Categories with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType ) , assetsWithCategory as ( select a.*, c.categoryDescription from assets a left outer join categories c on c.contentDomain = a.contentDomain and c.categoryId = a.categoryId ) ,industryCategories as ( SELECT categoryId, categoryDescription FROM tbl_PRD_RepositoryCategories with (nolock) WHERE containerType = 'SKU' AND ownerID = 0 AND ownerType = '*' AND left(categoryDescription, 3) <> 'GS1' ) , assetsWithIndustryCategory as ( select a.*, ic.categoryDescription as industryCategoryDescription from assetsWithCategory a left outer join industryCategories ic on ic.categoryId = a.categoryId ) , assetsWithPartner as ( select a.*, opr.companyName as partnerName, a.containerId as partnerId, a.containerType as partnerType, opr.territoryId from assetsWithIndustryCategory a inner join tbl_OPR_CLientOperators opr with (nolock) on opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = a.containerId where a.containerType = 'OPR' union all select a.*, cdr.cdr_dstName as partnerName, a.containerId as partnerId, a.containerType as partnerType, cdr.cdr_territoryId as territoryId from assetsWithIndustryCategory a inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = @ownerId and cdr.fsl_tablecode = @ownerType and cdr.cdr_recordId = a.containerId where a.containerType = 'CDR' union all select a.*, mfr.mfr_name as partnerName, mfr.mfr_id as partnerId, 'MFR' as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a inner join tbl_BRO_Principals bpl with (nolock) on bpl.ownerId = @ownerId and bpl.ownerType = @ownerType and bpl.principalId = a.containerId inner join tblManufacturers mfr with (nolock) on mfr.mfr_id = bpl.mfrId where a.containerType = 'BPL' union all select a.*,cast (null as varchar ) as partnerName, cast( null as integer ) as partnerId, cast( null as varchar ) as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a where coalesce( a.containerType, '???') not in ( 'BPL', 'OPR', 'CDR' ) ), assetsWithUser as ( select a.*, m.email updateUserEmail, m.firstName + ' ' + m.lastName as updateUserFullName, case when len(isnull(a.linked_prd,'')) > 0 or len(isnull(a.linked_sku,'')) > 0 then 'Y' else 'N' end as prdLink, case when len(isnull(a.linked_opr,'')) > 0 then 'Y' else 'N' end as oprLink, case when len(isnull(a.linked_cdr,'')) > 0 then 'Y' else 'N' end as cdrLink, case when len(isnull(a.linked_mfr,'')) > 0 then 'Y' else 'N' end as mfrLink from assetsWithPartner as a left outer join tbl_Fspro_members m with (nolock) on m.ownerId > 0 and m.fspro_userId = a.updateUserId ) , assetsWithUser_mappedData as ( select * from assetsWithUser ) , assetsWithUser_preData as ( select * from assetsWithUser_mappedData with (nolock) where ( ( [fileId] = '18096' ) and ( ( 1 = 1 ) and ( ( ( [isPublished] = 'Y' ) or ( [isPublished] = 'F' ) ) ) ) ) ) , assetsWithUser_data as ( select * from assetsWithUser_preData ) , assetsWithUser_return as ( select * from assetsWithUser_data where 1 = 1 order by docTitle offset 0 rows fetch next 50 rows only ) , assetsWithUser_result as ( select ( select count(*) from assetsWithUser_data ) as totalCount, ( select * from assetsWithUser_return for json auto, include_null_values ) as data ) select * from assetsWithUser_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER -- 2026-04-16 17:40:07.637 -- 0 {ts '2026-04-16 17:40:09'} SQL declare @ownerId int = 207 ; declare @ownerType varchar(3) = 'MFR' ; declare @contentPrefix varchar(128) = 'http://dev.fsenablers.com/FSLContent/' ; declare @todayDate datetime = ?; with docCategories as ( SELECT cats.attrID as categoryId, cats.attrDescription AS categoryName FROM tbl_ORG_Attributes cats with (nolock) WHERE cats.OwnerID = @ownerId AND cats.FSL_TableCode = @ownerType AND cats.attrTypeID = 31 ), rawAssets as ( select null contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, cast( null as varchar ) docTitle, cast( null as int) as categoryId, cast( null as int ) contentId, cast( null as int) fileId, 0.00 fileSize, null fileExt, null docDescription, cast( null as datetime ) publishDate, cast( null as datetime ) unpublishDate, 'N' isPublished, cast( null as datetime ) uploadDate, cast( null as datetime ) updateDate, cast( null as int ) updateUserId, 0 restricted, -1 thumbnailId, -1 highresCntId, 99 as sortRank , 0 as freshness , 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr, cast( null as varchar ) as assetSelectionKey, 0 as selectedRecord , 'N' as internalOnly, '' as recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds union all select 'DOC' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, d.docTitle, d.repCategoryId as categoryId, d.contentId, d.docId as fileId, CAST((d.fileSize/1024) AS float) as fileSize , d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, case when d.publishDate is not null and d.publishDate > @todayDate then 'F' when d.publishDate is not null and d.unpublishDate is not null and @todayDate >= d.publishDate and @todayDate <= d.unpublishDate then 'Y' when d.publishDate is not null and d.unpublishDate is null and d.publishDate <= @todayDate then 'Y' when d.publishDate is null then 'N' else 'N' end isPublished, d.uploadDate, d.lastUpdatedDate updateDate, d.lastUpdatedBy updateUserId, 0 restricted, thumbnailId, -1 highresCntId, sortRank , coalesce(dateDiff(day, d.lastUpdatedDate, @todayDate),0) as freshness, case when d.privateYesNo = 1 then 'Y' else 'N' end privateYesNo, ou.firstname + ' ' + ou.lastname as uploadedByFullName, d.keyWords, d.unpublishToDo, d.internalNotes , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END), ',') AS linked_opr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END), ',') AS linked_prd , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END), ',') AS linked_sku , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END), ',') AS linked_cdr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END), ',') AS linked_mfr , cast(concat('DOC',':',d.docId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , d.internalOnly, d.recordSource , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 23 THEN li.child_attrID ELSE NULL END), ',') AS permissionGrpIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 7 THEN li.child_attrID ELSE NULL END), ',') AS linked_brokerIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 31 THEN li.child_attrID ELSE NULL END), ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = d.docId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 34 THEN li.child_attrID ELSE NULL END), ',') AS linked_UserMemberIds from tbl_DOC_Documents d with (nolock) LEFT OUTER JOIN tbl_FSPRO_Members ou with (nolock) ON d.AuthorUserId = ou.fspro_userId left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = d.docId and li.child_attrTypeID in (41,59,60,63,42,23,7,31,34) and li.mstr_attrTypeID = 30 left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('DOC',':',d.docId) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType GROUP BY d.docTitle, d.repCategoryId, d.contentId, d.docId,d.fileSize, d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, d.uploadDate, d.lastUpdatedDate, d.lastUpdatedBy,thumbnailId, sortRank , d.privateYesNo , ou.firstname,ou.lastname, d.keyWords, d.unpublishToDo, d.internalNotes,ast.assetSelectionKey, d.internalOnly, d.recordSource union all select 'IMG' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, i.imgTitle as docTitle, i.categoryId, i.contentId, i.imgId as fileId, CAST((i.fileSize/1024) AS float) as fileSize, i.fileExt, i.imgDescription as docDescription, i.publishDate, i.unpublishDate, case when i.publishDate is not null and i.publishDate > @todayDate then 'F' when i.publishDate is not null and i.unpublishDate is not null and @todayDate >= i.publishDate and @todayDate <= i.unpublishDate then 'Y' when i.publishDate is not null and i.unpublishDate is null and i.publishDate <= @todayDate then 'Y' when i.publishDate is null then 'N' else 'N' end isPublished, i.uploadDate, i.lastUpdatedDate updateDate, i.lastUpdatedBy updateUserId, 0 restricted, -1 thumbnailId, i.highresCntId, i.sortRank, coalesce(dateDiff(day, lastUpdatedDate, @todayDate),0) as freshness, case when i.privateYesNo = 1 then 'Y' else 'N' end privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , STRING_AGG(CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END, ',') AS linked_opr , STRING_AGG(CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END, ',') AS linked_prd , STRING_AGG(CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END, ',') AS linked_sku , STRING_AGG(CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END, ',') AS linked_cdr , STRING_AGG(CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END, ',') AS linked_mfr , cast(concat('IMG',':',i.imgId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , i.internalOnly, i.recordSource , STRING_AGG(CASE WHEN c.child_attrTypeID = 23 THEN c.child_attrID ELSE NULL END, ',') AS permissionGrpIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 7 THEN c.child_attrID ELSE NULL END, ',') AS linked_brokerIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 31 THEN c.child_attrID ELSE NULL END, ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = i.imgId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CASE WHEN c.child_attrTypeID = 34 THEN c.child_attrID ELSE NULL END, ',') AS linked_UserMemberIds from tbl_IMG_Images i with (nolock) left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = i.imgId and li.child_attrTypeID in (41,59,60,63,42) and li.mstr_attrTypeID = 33 left outer JOIN tbl_ORG_AttributeLinks c ON c.mstr_attrID = i.imgId and c.child_attrTypeID in (23,7,31,34) and c.mstr_attrTypeID = 30 left outer join tbl_DOC_Categories dc with (nolock) ON c.child_attrTypeID = 31 AND dc.categoryId = c.child_attrID AND dc.ownerId = @ownerId AND dc.fsl_tablecode = @ownerType left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('IMG',':',i.imgId) where i.ownerId = @ownerId and i.fsl_tablecode = @ownerType GROUP BY i.imgTitle, i.CategoryID, i.contentId, i.imgId, i.fileSize, i.fileExt, i.imgDescription, i.publishDate, i.unpublishDate, i.uploadDate, i.lastUpdatedDate, i.lastUpdatedBy, i.highresCntId, i.sortRank, i.privateYesNo,ast.assetSelectionKey, i.internalOnly, i.recordSource ), filteredRawAssets as ( select ra.* from rawAssets ra where (1=1 or ra.linked_brokerIds in (?) ) ) , assetsWithContentURL1 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL, c.fileName as docFileName, lub.firstname + ' ' + lub.lastname as lastUpdatedByFullName from filteredRawAssets ra LEFT OUTER JOIN tbl_FSPRO_Members lub with (nolock) ON ra.updateUserId = lub.fspro_userId inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) , assetsWithContentURL2 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as thumbnailURL, c.fileName as thnFileName from assetsWithContentURL1 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.thumbnailId and c.contentId != 755 ) , assetsWithContentURL3 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as highresURL, c.fileName as highresCntFileName from assetsWithContentURL2 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.highresCntId and c.contentId != 755 ) , assets as ( select a.*, case when right( a.contentURL, charindex( '.', reverse( a.contentURL )) - 1 ) in ( 'BMP','GIF','JPEG','JPG','PNG','TIF','TIFF','WBMP' ) then left( a.contentURL, len( a.contentURL ) - charindex( '.', reverse( a.contentURL ) )) + '_50' + right( a.contentURL, charindex( '.', reverse( a.contentURL )) ) else null end previewURL from assetsWithContentURL3 a ) , categories as ( select 'OPR' contentDomain, categoryId, categoryDescription from tbl_OPR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'CDR' contentDomain, categoryId, categoryDescription from tbl_CDR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'BPL' contentDomain, categoryId, categoryDescription from tbl_BPL_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'DOC' contentDomain, categoryId, categoryName as categoryDescription from tbl_DOC_Categories with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType ) , assetsWithCategory as ( select a.*, c.categoryDescription from assets a left outer join categories c on c.contentDomain = a.contentDomain and c.categoryId = a.categoryId ) ,industryCategories as ( SELECT categoryId, categoryDescription FROM tbl_PRD_RepositoryCategories with (nolock) WHERE containerType = 'SKU' AND ownerID = 0 AND ownerType = '*' AND left(categoryDescription, 3) <> 'GS1' ) , assetsWithIndustryCategory as ( select a.*, ic.categoryDescription as industryCategoryDescription from assetsWithCategory a left outer join industryCategories ic on ic.categoryId = a.categoryId ) , assetsWithPartner as ( select a.*, opr.companyName as partnerName, a.containerId as partnerId, a.containerType as partnerType, opr.territoryId from assetsWithIndustryCategory a inner join tbl_OPR_CLientOperators opr with (nolock) on opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = a.containerId where a.containerType = 'OPR' union all select a.*, cdr.cdr_dstName as partnerName, a.containerId as partnerId, a.containerType as partnerType, cdr.cdr_territoryId as territoryId from assetsWithIndustryCategory a inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = @ownerId and cdr.fsl_tablecode = @ownerType and cdr.cdr_recordId = a.containerId where a.containerType = 'CDR' union all select a.*, mfr.mfr_name as partnerName, mfr.mfr_id as partnerId, 'MFR' as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a inner join tbl_BRO_Principals bpl with (nolock) on bpl.ownerId = @ownerId and bpl.ownerType = @ownerType and bpl.principalId = a.containerId inner join tblManufacturers mfr with (nolock) on mfr.mfr_id = bpl.mfrId where a.containerType = 'BPL' union all select a.*,cast (null as varchar ) as partnerName, cast( null as integer ) as partnerId, cast( null as varchar ) as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a where coalesce( a.containerType, '???') not in ( 'BPL', 'OPR', 'CDR' ) ), assetsWithUser as ( select a.*, m.email updateUserEmail, m.firstName + ' ' + m.lastName as updateUserFullName, case when len(isnull(a.linked_prd,'')) > 0 or len(isnull(a.linked_sku,'')) > 0 then 'Y' else 'N' end as prdLink, case when len(isnull(a.linked_opr,'')) > 0 then 'Y' else 'N' end as oprLink, case when len(isnull(a.linked_cdr,'')) > 0 then 'Y' else 'N' end as cdrLink, case when len(isnull(a.linked_mfr,'')) > 0 then 'Y' else 'N' end as mfrLink from assetsWithPartner as a left outer join tbl_Fspro_members m with (nolock) on m.ownerId > 0 and m.fspro_userId = a.updateUserId ) , assetsWithUser_mappedData as ( select * from assetsWithUser ) , assetsWithUser_preData as ( select * from assetsWithUser_mappedData with (nolock) where ( ( [fileId] = '1809' ) and ( ( 1 = 1 ) and ( ( ( [isPublished] = 'Y' ) or ( [isPublished] = 'F' ) ) ) ) ) ) , assetsWithUser_data as ( select * from assetsWithUser_preData ) , assetsWithUser_return as ( select * from assetsWithUser_data where 1 = 1 order by docTitle offset 0 rows fetch next 50 rows only ) , assetsWithUser_result as ( select ( select count(*) from assetsWithUser_data ) as totalCount, ( select * from assetsWithUser_return for json auto, include_null_values ) as data ) select * from assetsWithUser_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER -- 2026-04-16 17:40:09.281 -- 0 {ts '2026-04-16 17:40:10'} SQL declare @ownerId int = 207 ; declare @ownerType varchar(3) = 'MFR' ; declare @contentPrefix varchar(128) = 'http://dev.fsenablers.com/FSLContent/' ; declare @todayDate datetime = ?; with docCategories as ( SELECT cats.attrID as categoryId, cats.attrDescription AS categoryName FROM tbl_ORG_Attributes cats with (nolock) WHERE cats.OwnerID = @ownerId AND cats.FSL_TableCode = @ownerType AND cats.attrTypeID = 31 ), rawAssets as ( select null contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, cast( null as varchar ) docTitle, cast( null as int) as categoryId, cast( null as int ) contentId, cast( null as int) fileId, 0.00 fileSize, null fileExt, null docDescription, cast( null as datetime ) publishDate, cast( null as datetime ) unpublishDate, 'N' isPublished, cast( null as datetime ) uploadDate, cast( null as datetime ) updateDate, cast( null as int ) updateUserId, 0 restricted, -1 thumbnailId, -1 highresCntId, 99 as sortRank , 0 as freshness , 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr, cast( null as varchar ) as assetSelectionKey, 0 as selectedRecord , 'N' as internalOnly, '' as recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds union all select 'DOC' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, d.docTitle, d.repCategoryId as categoryId, d.contentId, d.docId as fileId, CAST((d.fileSize/1024) AS float) as fileSize , d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, case when d.publishDate is not null and d.publishDate > @todayDate then 'F' when d.publishDate is not null and d.unpublishDate is not null and @todayDate >= d.publishDate and @todayDate <= d.unpublishDate then 'Y' when d.publishDate is not null and d.unpublishDate is null and d.publishDate <= @todayDate then 'Y' when d.publishDate is null then 'N' else 'N' end isPublished, d.uploadDate, d.lastUpdatedDate updateDate, d.lastUpdatedBy updateUserId, 0 restricted, thumbnailId, -1 highresCntId, sortRank , coalesce(dateDiff(day, d.lastUpdatedDate, @todayDate),0) as freshness, case when d.privateYesNo = 1 then 'Y' else 'N' end privateYesNo, ou.firstname + ' ' + ou.lastname as uploadedByFullName, d.keyWords, d.unpublishToDo, d.internalNotes , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END), ',') AS linked_opr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END), ',') AS linked_prd , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END), ',') AS linked_sku , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END), ',') AS linked_cdr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END), ',') AS linked_mfr , cast(concat('DOC',':',d.docId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , d.internalOnly, d.recordSource , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 23 THEN li.child_attrID ELSE NULL END), ',') AS permissionGrpIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 7 THEN li.child_attrID ELSE NULL END), ',') AS linked_brokerIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 31 THEN li.child_attrID ELSE NULL END), ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = d.docId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 34 THEN li.child_attrID ELSE NULL END), ',') AS linked_UserMemberIds from tbl_DOC_Documents d with (nolock) LEFT OUTER JOIN tbl_FSPRO_Members ou with (nolock) ON d.AuthorUserId = ou.fspro_userId left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = d.docId and li.child_attrTypeID in (41,59,60,63,42,23,7,31,34) and li.mstr_attrTypeID = 30 left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('DOC',':',d.docId) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType GROUP BY d.docTitle, d.repCategoryId, d.contentId, d.docId,d.fileSize, d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, d.uploadDate, d.lastUpdatedDate, d.lastUpdatedBy,thumbnailId, sortRank , d.privateYesNo , ou.firstname,ou.lastname, d.keyWords, d.unpublishToDo, d.internalNotes,ast.assetSelectionKey, d.internalOnly, d.recordSource union all select 'IMG' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, i.imgTitle as docTitle, i.categoryId, i.contentId, i.imgId as fileId, CAST((i.fileSize/1024) AS float) as fileSize, i.fileExt, i.imgDescription as docDescription, i.publishDate, i.unpublishDate, case when i.publishDate is not null and i.publishDate > @todayDate then 'F' when i.publishDate is not null and i.unpublishDate is not null and @todayDate >= i.publishDate and @todayDate <= i.unpublishDate then 'Y' when i.publishDate is not null and i.unpublishDate is null and i.publishDate <= @todayDate then 'Y' when i.publishDate is null then 'N' else 'N' end isPublished, i.uploadDate, i.lastUpdatedDate updateDate, i.lastUpdatedBy updateUserId, 0 restricted, -1 thumbnailId, i.highresCntId, i.sortRank, coalesce(dateDiff(day, lastUpdatedDate, @todayDate),0) as freshness, case when i.privateYesNo = 1 then 'Y' else 'N' end privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , STRING_AGG(CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END, ',') AS linked_opr , STRING_AGG(CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END, ',') AS linked_prd , STRING_AGG(CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END, ',') AS linked_sku , STRING_AGG(CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END, ',') AS linked_cdr , STRING_AGG(CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END, ',') AS linked_mfr , cast(concat('IMG',':',i.imgId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , i.internalOnly, i.recordSource , STRING_AGG(CASE WHEN c.child_attrTypeID = 23 THEN c.child_attrID ELSE NULL END, ',') AS permissionGrpIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 7 THEN c.child_attrID ELSE NULL END, ',') AS linked_brokerIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 31 THEN c.child_attrID ELSE NULL END, ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = i.imgId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CASE WHEN c.child_attrTypeID = 34 THEN c.child_attrID ELSE NULL END, ',') AS linked_UserMemberIds from tbl_IMG_Images i with (nolock) left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = i.imgId and li.child_attrTypeID in (41,59,60,63,42) and li.mstr_attrTypeID = 33 left outer JOIN tbl_ORG_AttributeLinks c ON c.mstr_attrID = i.imgId and c.child_attrTypeID in (23,7,31,34) and c.mstr_attrTypeID = 30 left outer join tbl_DOC_Categories dc with (nolock) ON c.child_attrTypeID = 31 AND dc.categoryId = c.child_attrID AND dc.ownerId = @ownerId AND dc.fsl_tablecode = @ownerType left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('IMG',':',i.imgId) where i.ownerId = @ownerId and i.fsl_tablecode = @ownerType GROUP BY i.imgTitle, i.CategoryID, i.contentId, i.imgId, i.fileSize, i.fileExt, i.imgDescription, i.publishDate, i.unpublishDate, i.uploadDate, i.lastUpdatedDate, i.lastUpdatedBy, i.highresCntId, i.sortRank, i.privateYesNo,ast.assetSelectionKey, i.internalOnly, i.recordSource ), filteredRawAssets as ( select ra.* from rawAssets ra where (1=1 or ra.linked_brokerIds in (?) ) ) , assetsWithContentURL1 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL, c.fileName as docFileName, lub.firstname + ' ' + lub.lastname as lastUpdatedByFullName from filteredRawAssets ra LEFT OUTER JOIN tbl_FSPRO_Members lub with (nolock) ON ra.updateUserId = lub.fspro_userId inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) , assetsWithContentURL2 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as thumbnailURL, c.fileName as thnFileName from assetsWithContentURL1 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.thumbnailId and c.contentId != 755 ) , assetsWithContentURL3 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as highresURL, c.fileName as highresCntFileName from assetsWithContentURL2 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.highresCntId and c.contentId != 755 ) , assets as ( select a.*, case when right( a.contentURL, charindex( '.', reverse( a.contentURL )) - 1 ) in ( 'BMP','GIF','JPEG','JPG','PNG','TIF','TIFF','WBMP' ) then left( a.contentURL, len( a.contentURL ) - charindex( '.', reverse( a.contentURL ) )) + '_50' + right( a.contentURL, charindex( '.', reverse( a.contentURL )) ) else null end previewURL from assetsWithContentURL3 a ) , categories as ( select 'OPR' contentDomain, categoryId, categoryDescription from tbl_OPR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'CDR' contentDomain, categoryId, categoryDescription from tbl_CDR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'BPL' contentDomain, categoryId, categoryDescription from tbl_BPL_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'DOC' contentDomain, categoryId, categoryName as categoryDescription from tbl_DOC_Categories with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType ) , assetsWithCategory as ( select a.*, c.categoryDescription from assets a left outer join categories c on c.contentDomain = a.contentDomain and c.categoryId = a.categoryId ) ,industryCategories as ( SELECT categoryId, categoryDescription FROM tbl_PRD_RepositoryCategories with (nolock) WHERE containerType = 'SKU' AND ownerID = 0 AND ownerType = '*' AND left(categoryDescription, 3) <> 'GS1' ) , assetsWithIndustryCategory as ( select a.*, ic.categoryDescription as industryCategoryDescription from assetsWithCategory a left outer join industryCategories ic on ic.categoryId = a.categoryId ) , assetsWithPartner as ( select a.*, opr.companyName as partnerName, a.containerId as partnerId, a.containerType as partnerType, opr.territoryId from assetsWithIndustryCategory a inner join tbl_OPR_CLientOperators opr with (nolock) on opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = a.containerId where a.containerType = 'OPR' union all select a.*, cdr.cdr_dstName as partnerName, a.containerId as partnerId, a.containerType as partnerType, cdr.cdr_territoryId as territoryId from assetsWithIndustryCategory a inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = @ownerId and cdr.fsl_tablecode = @ownerType and cdr.cdr_recordId = a.containerId where a.containerType = 'CDR' union all select a.*, mfr.mfr_name as partnerName, mfr.mfr_id as partnerId, 'MFR' as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a inner join tbl_BRO_Principals bpl with (nolock) on bpl.ownerId = @ownerId and bpl.ownerType = @ownerType and bpl.principalId = a.containerId inner join tblManufacturers mfr with (nolock) on mfr.mfr_id = bpl.mfrId where a.containerType = 'BPL' union all select a.*,cast (null as varchar ) as partnerName, cast( null as integer ) as partnerId, cast( null as varchar ) as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a where coalesce( a.containerType, '???') not in ( 'BPL', 'OPR', 'CDR' ) ), assetsWithUser as ( select a.*, m.email updateUserEmail, m.firstName + ' ' + m.lastName as updateUserFullName, case when len(isnull(a.linked_prd,'')) > 0 or len(isnull(a.linked_sku,'')) > 0 then 'Y' else 'N' end as prdLink, case when len(isnull(a.linked_opr,'')) > 0 then 'Y' else 'N' end as oprLink, case when len(isnull(a.linked_cdr,'')) > 0 then 'Y' else 'N' end as cdrLink, case when len(isnull(a.linked_mfr,'')) > 0 then 'Y' else 'N' end as mfrLink from assetsWithPartner as a left outer join tbl_Fspro_members m with (nolock) on m.ownerId > 0 and m.fspro_userId = a.updateUserId ) , assetsWithUser_mappedData as ( select * from assetsWithUser ) , assetsWithUser_preData as ( select * from assetsWithUser_mappedData with (nolock) where ( ( [fileId] = '180946' ) and ( ( 1 = 1 ) and ( ( ( [isPublished] = 'Y' ) or ( [isPublished] = 'F' ) ) ) ) ) ) , assetsWithUser_data as ( select * from assetsWithUser_preData ) , assetsWithUser_return as ( select * from assetsWithUser_data where 1 = 1 order by docTitle offset 0 rows fetch next 50 rows only ) , assetsWithUser_result as ( select ( select count(*) from assetsWithUser_data ) as totalCount, ( select * from assetsWithUser_return for json auto, include_null_values ) as data ) select * from assetsWithUser_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER -- 2026-04-16 17:40:10.764 -- 0 {ts '2026-04-16 17:40:13'} SQL declare @ownerId int = 207 ; declare @ownerType varchar(3) = 'MFR' ; declare @contentPrefix varchar(128) = 'http://dev.fsenablers.com/FSLContent/' ; declare @todayDate datetime = ?; with docCategories as ( SELECT cats.attrID as categoryId, cats.attrDescription AS categoryName FROM tbl_ORG_Attributes cats with (nolock) WHERE cats.OwnerID = @ownerId AND cats.FSL_TableCode = @ownerType AND cats.attrTypeID = 31 ), rawAssets as ( select null contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, cast( null as varchar ) docTitle, cast( null as int) as categoryId, cast( null as int ) contentId, cast( null as int) fileId, 0.00 fileSize, null fileExt, null docDescription, cast( null as datetime ) publishDate, cast( null as datetime ) unpublishDate, 'N' isPublished, cast( null as datetime ) uploadDate, cast( null as datetime ) updateDate, cast( null as int ) updateUserId, 0 restricted, -1 thumbnailId, -1 highresCntId, 99 as sortRank , 0 as freshness , 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr, cast( null as varchar ) as assetSelectionKey, 0 as selectedRecord , 'N' as internalOnly, '' as recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds union all select 'DOC' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, d.docTitle, d.repCategoryId as categoryId, d.contentId, d.docId as fileId, CAST((d.fileSize/1024) AS float) as fileSize , d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, case when d.publishDate is not null and d.publishDate > @todayDate then 'F' when d.publishDate is not null and d.unpublishDate is not null and @todayDate >= d.publishDate and @todayDate <= d.unpublishDate then 'Y' when d.publishDate is not null and d.unpublishDate is null and d.publishDate <= @todayDate then 'Y' when d.publishDate is null then 'N' else 'N' end isPublished, d.uploadDate, d.lastUpdatedDate updateDate, d.lastUpdatedBy updateUserId, 0 restricted, thumbnailId, -1 highresCntId, sortRank , coalesce(dateDiff(day, d.lastUpdatedDate, @todayDate),0) as freshness, case when d.privateYesNo = 1 then 'Y' else 'N' end privateYesNo, ou.firstname + ' ' + ou.lastname as uploadedByFullName, d.keyWords, d.unpublishToDo, d.internalNotes , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END), ',') AS linked_opr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END), ',') AS linked_prd , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END), ',') AS linked_sku , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END), ',') AS linked_cdr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END), ',') AS linked_mfr , cast(concat('DOC',':',d.docId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , d.internalOnly, d.recordSource , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 23 THEN li.child_attrID ELSE NULL END), ',') AS permissionGrpIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 7 THEN li.child_attrID ELSE NULL END), ',') AS linked_brokerIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 31 THEN li.child_attrID ELSE NULL END), ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = d.docId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 34 THEN li.child_attrID ELSE NULL END), ',') AS linked_UserMemberIds from tbl_DOC_Documents d with (nolock) LEFT OUTER JOIN tbl_FSPRO_Members ou with (nolock) ON d.AuthorUserId = ou.fspro_userId left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = d.docId and li.child_attrTypeID in (41,59,60,63,42,23,7,31,34) and li.mstr_attrTypeID = 30 left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('DOC',':',d.docId) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType GROUP BY d.docTitle, d.repCategoryId, d.contentId, d.docId,d.fileSize, d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, d.uploadDate, d.lastUpdatedDate, d.lastUpdatedBy,thumbnailId, sortRank , d.privateYesNo , ou.firstname,ou.lastname, d.keyWords, d.unpublishToDo, d.internalNotes,ast.assetSelectionKey, d.internalOnly, d.recordSource union all select 'IMG' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, i.imgTitle as docTitle, i.categoryId, i.contentId, i.imgId as fileId, CAST((i.fileSize/1024) AS float) as fileSize, i.fileExt, i.imgDescription as docDescription, i.publishDate, i.unpublishDate, case when i.publishDate is not null and i.publishDate > @todayDate then 'F' when i.publishDate is not null and i.unpublishDate is not null and @todayDate >= i.publishDate and @todayDate <= i.unpublishDate then 'Y' when i.publishDate is not null and i.unpublishDate is null and i.publishDate <= @todayDate then 'Y' when i.publishDate is null then 'N' else 'N' end isPublished, i.uploadDate, i.lastUpdatedDate updateDate, i.lastUpdatedBy updateUserId, 0 restricted, -1 thumbnailId, i.highresCntId, i.sortRank, coalesce(dateDiff(day, lastUpdatedDate, @todayDate),0) as freshness, case when i.privateYesNo = 1 then 'Y' else 'N' end privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , STRING_AGG(CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END, ',') AS linked_opr , STRING_AGG(CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END, ',') AS linked_prd , STRING_AGG(CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END, ',') AS linked_sku , STRING_AGG(CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END, ',') AS linked_cdr , STRING_AGG(CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END, ',') AS linked_mfr , cast(concat('IMG',':',i.imgId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , i.internalOnly, i.recordSource , STRING_AGG(CASE WHEN c.child_attrTypeID = 23 THEN c.child_attrID ELSE NULL END, ',') AS permissionGrpIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 7 THEN c.child_attrID ELSE NULL END, ',') AS linked_brokerIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 31 THEN c.child_attrID ELSE NULL END, ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = i.imgId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CASE WHEN c.child_attrTypeID = 34 THEN c.child_attrID ELSE NULL END, ',') AS linked_UserMemberIds from tbl_IMG_Images i with (nolock) left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = i.imgId and li.child_attrTypeID in (41,59,60,63,42) and li.mstr_attrTypeID = 33 left outer JOIN tbl_ORG_AttributeLinks c ON c.mstr_attrID = i.imgId and c.child_attrTypeID in (23,7,31,34) and c.mstr_attrTypeID = 30 left outer join tbl_DOC_Categories dc with (nolock) ON c.child_attrTypeID = 31 AND dc.categoryId = c.child_attrID AND dc.ownerId = @ownerId AND dc.fsl_tablecode = @ownerType left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('IMG',':',i.imgId) where i.ownerId = @ownerId and i.fsl_tablecode = @ownerType GROUP BY i.imgTitle, i.CategoryID, i.contentId, i.imgId, i.fileSize, i.fileExt, i.imgDescription, i.publishDate, i.unpublishDate, i.uploadDate, i.lastUpdatedDate, i.lastUpdatedBy, i.highresCntId, i.sortRank, i.privateYesNo,ast.assetSelectionKey, i.internalOnly, i.recordSource ), filteredRawAssets as ( select ra.* from rawAssets ra where (1=1 or ra.linked_brokerIds in (?) ) ) , assetsWithContentURL1 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL, c.fileName as docFileName, lub.firstname + ' ' + lub.lastname as lastUpdatedByFullName from filteredRawAssets ra LEFT OUTER JOIN tbl_FSPRO_Members lub with (nolock) ON ra.updateUserId = lub.fspro_userId inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) , assetsWithContentURL2 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as thumbnailURL, c.fileName as thnFileName from assetsWithContentURL1 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.thumbnailId and c.contentId != 755 ) , assetsWithContentURL3 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as highresURL, c.fileName as highresCntFileName from assetsWithContentURL2 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.highresCntId and c.contentId != 755 ) , assets as ( select a.*, case when right( a.contentURL, charindex( '.', reverse( a.contentURL )) - 1 ) in ( 'BMP','GIF','JPEG','JPG','PNG','TIF','TIFF','WBMP' ) then left( a.contentURL, len( a.contentURL ) - charindex( '.', reverse( a.contentURL ) )) + '_50' + right( a.contentURL, charindex( '.', reverse( a.contentURL )) ) else null end previewURL from assetsWithContentURL3 a ) , categories as ( select 'OPR' contentDomain, categoryId, categoryDescription from tbl_OPR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'CDR' contentDomain, categoryId, categoryDescription from tbl_CDR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'BPL' contentDomain, categoryId, categoryDescription from tbl_BPL_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'DOC' contentDomain, categoryId, categoryName as categoryDescription from tbl_DOC_Categories with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType ) , assetsWithCategory as ( select a.*, c.categoryDescription from assets a left outer join categories c on c.contentDomain = a.contentDomain and c.categoryId = a.categoryId ) ,industryCategories as ( SELECT categoryId, categoryDescription FROM tbl_PRD_RepositoryCategories with (nolock) WHERE containerType = 'SKU' AND ownerID = 0 AND ownerType = '*' AND left(categoryDescription, 3) <> 'GS1' ) , assetsWithIndustryCategory as ( select a.*, ic.categoryDescription as industryCategoryDescription from assetsWithCategory a left outer join industryCategories ic on ic.categoryId = a.categoryId ) , assetsWithPartner as ( select a.*, opr.companyName as partnerName, a.containerId as partnerId, a.containerType as partnerType, opr.territoryId from assetsWithIndustryCategory a inner join tbl_OPR_CLientOperators opr with (nolock) on opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = a.containerId where a.containerType = 'OPR' union all select a.*, cdr.cdr_dstName as partnerName, a.containerId as partnerId, a.containerType as partnerType, cdr.cdr_territoryId as territoryId from assetsWithIndustryCategory a inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = @ownerId and cdr.fsl_tablecode = @ownerType and cdr.cdr_recordId = a.containerId where a.containerType = 'CDR' union all select a.*, mfr.mfr_name as partnerName, mfr.mfr_id as partnerId, 'MFR' as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a inner join tbl_BRO_Principals bpl with (nolock) on bpl.ownerId = @ownerId and bpl.ownerType = @ownerType and bpl.principalId = a.containerId inner join tblManufacturers mfr with (nolock) on mfr.mfr_id = bpl.mfrId where a.containerType = 'BPL' union all select a.*,cast (null as varchar ) as partnerName, cast( null as integer ) as partnerId, cast( null as varchar ) as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a where coalesce( a.containerType, '???') not in ( 'BPL', 'OPR', 'CDR' ) ), assetsWithUser as ( select a.*, m.email updateUserEmail, m.firstName + ' ' + m.lastName as updateUserFullName, case when len(isnull(a.linked_prd,'')) > 0 or len(isnull(a.linked_sku,'')) > 0 then 'Y' else 'N' end as prdLink, case when len(isnull(a.linked_opr,'')) > 0 then 'Y' else 'N' end as oprLink, case when len(isnull(a.linked_cdr,'')) > 0 then 'Y' else 'N' end as cdrLink, case when len(isnull(a.linked_mfr,'')) > 0 then 'Y' else 'N' end as mfrLink from assetsWithPartner as a left outer join tbl_Fspro_members m with (nolock) on m.ownerId > 0 and m.fspro_userId = a.updateUserId ) , assetsWithUser_mappedData as ( select * from assetsWithUser ) , assetsWithUser_preData as ( select * from assetsWithUser_mappedData with (nolock) where ( ( [fileId] = '180964' ) and ( ( 1 = 1 ) and ( ( ( [isPublished] = 'Y' ) or ( [isPublished] = 'F' ) ) ) ) ) ) , assetsWithUser_data as ( select * from assetsWithUser_preData ) , assetsWithUser_return as ( select * from assetsWithUser_data where 1 = 1 order by docTitle offset 0 rows fetch next 50 rows only ) , assetsWithUser_result as ( select ( select count(*) from assetsWithUser_data ) as totalCount, ( select * from assetsWithUser_return for json auto, include_null_values ) as data ) select * from assetsWithUser_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER -- 2026-04-16 17:40:13.17 -- 0 {ts '2026-04-16 19:49:18'} SQL declare @ownerId int = 207 ; declare @ownerType varchar(3) = 'MFR' ; declare @contentPrefix varchar(128) = 'http://dev.fsenablers.com/FSLContent/' ; declare @todayDate datetime = ?; with docCategories as ( SELECT cats.attrID as categoryId, cats.attrDescription AS categoryName FROM tbl_ORG_Attributes cats with (nolock) WHERE cats.OwnerID = @ownerId AND cats.FSL_TableCode = @ownerType AND cats.attrTypeID = 31 ), rawAssets as ( select null contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, cast( null as varchar ) docTitle, cast( null as int) as categoryId, cast( null as int ) contentId, cast( null as int) fileId, 0.00 fileSize, null fileExt, null docDescription, cast( null as datetime ) publishDate, cast( null as datetime ) unpublishDate, 'N' isPublished, cast( null as datetime ) uploadDate, cast( null as datetime ) updateDate, cast( null as int ) updateUserId, 0 restricted, -1 thumbnailId, -1 highresCntId, 99 as sortRank , 0 as freshness , 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr, cast( null as varchar ) as assetSelectionKey, 0 as selectedRecord , 'N' as internalOnly, '' as recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds union all select 'DOC' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, d.docTitle, d.repCategoryId as categoryId, d.contentId, d.docId as fileId, CAST((d.fileSize/1024) AS float) as fileSize , d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, case when d.publishDate is not null and d.publishDate > @todayDate then 'F' when d.publishDate is not null and d.unpublishDate is not null and @todayDate >= d.publishDate and @todayDate <= d.unpublishDate then 'Y' when d.publishDate is not null and d.unpublishDate is null and d.publishDate <= @todayDate then 'Y' when d.publishDate is null then 'N' else 'N' end isPublished, d.uploadDate, d.lastUpdatedDate updateDate, d.lastUpdatedBy updateUserId, 0 restricted, thumbnailId, -1 highresCntId, sortRank , coalesce(dateDiff(day, d.lastUpdatedDate, @todayDate),0) as freshness, case when d.privateYesNo = 1 then 'Y' else 'N' end privateYesNo, ou.firstname + ' ' + ou.lastname as uploadedByFullName, d.keyWords, d.unpublishToDo, d.internalNotes , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END), ',') AS linked_opr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END), ',') AS linked_prd , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END), ',') AS linked_sku , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END), ',') AS linked_cdr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END), ',') AS linked_mfr , cast(concat('DOC',':',d.docId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , d.internalOnly, d.recordSource , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 23 THEN li.child_attrID ELSE NULL END), ',') AS permissionGrpIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 7 THEN li.child_attrID ELSE NULL END), ',') AS linked_brokerIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 31 THEN li.child_attrID ELSE NULL END), ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = d.docId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 34 THEN li.child_attrID ELSE NULL END), ',') AS linked_UserMemberIds from tbl_DOC_Documents d with (nolock) LEFT OUTER JOIN tbl_FSPRO_Members ou with (nolock) ON d.AuthorUserId = ou.fspro_userId left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = d.docId and li.child_attrTypeID in (41,59,60,63,42,23,7,31,34) and li.mstr_attrTypeID = 30 left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('DOC',':',d.docId) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType GROUP BY d.docTitle, d.repCategoryId, d.contentId, d.docId,d.fileSize, d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, d.uploadDate, d.lastUpdatedDate, d.lastUpdatedBy,thumbnailId, sortRank , d.privateYesNo , ou.firstname,ou.lastname, d.keyWords, d.unpublishToDo, d.internalNotes,ast.assetSelectionKey, d.internalOnly, d.recordSource union all select 'IMG' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, i.imgTitle as docTitle, i.categoryId, i.contentId, i.imgId as fileId, CAST((i.fileSize/1024) AS float) as fileSize, i.fileExt, i.imgDescription as docDescription, i.publishDate, i.unpublishDate, case when i.publishDate is not null and i.publishDate > @todayDate then 'F' when i.publishDate is not null and i.unpublishDate is not null and @todayDate >= i.publishDate and @todayDate <= i.unpublishDate then 'Y' when i.publishDate is not null and i.unpublishDate is null and i.publishDate <= @todayDate then 'Y' when i.publishDate is null then 'N' else 'N' end isPublished, i.uploadDate, i.lastUpdatedDate updateDate, i.lastUpdatedBy updateUserId, 0 restricted, -1 thumbnailId, i.highresCntId, i.sortRank, coalesce(dateDiff(day, lastUpdatedDate, @todayDate),0) as freshness, case when i.privateYesNo = 1 then 'Y' else 'N' end privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , STRING_AGG(CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END, ',') AS linked_opr , STRING_AGG(CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END, ',') AS linked_prd , STRING_AGG(CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END, ',') AS linked_sku , STRING_AGG(CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END, ',') AS linked_cdr , STRING_AGG(CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END, ',') AS linked_mfr , cast(concat('IMG',':',i.imgId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , i.internalOnly, i.recordSource , STRING_AGG(CASE WHEN c.child_attrTypeID = 23 THEN c.child_attrID ELSE NULL END, ',') AS permissionGrpIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 7 THEN c.child_attrID ELSE NULL END, ',') AS linked_brokerIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 31 THEN c.child_attrID ELSE NULL END, ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = i.imgId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CASE WHEN c.child_attrTypeID = 34 THEN c.child_attrID ELSE NULL END, ',') AS linked_UserMemberIds from tbl_IMG_Images i with (nolock) left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = i.imgId and li.child_attrTypeID in (41,59,60,63,42) and li.mstr_attrTypeID = 33 left outer JOIN tbl_ORG_AttributeLinks c ON c.mstr_attrID = i.imgId and c.child_attrTypeID in (23,7,31,34) and c.mstr_attrTypeID = 30 left outer join tbl_DOC_Categories dc with (nolock) ON c.child_attrTypeID = 31 AND dc.categoryId = c.child_attrID AND dc.ownerId = @ownerId AND dc.fsl_tablecode = @ownerType left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('IMG',':',i.imgId) where i.ownerId = @ownerId and i.fsl_tablecode = @ownerType GROUP BY i.imgTitle, i.CategoryID, i.contentId, i.imgId, i.fileSize, i.fileExt, i.imgDescription, i.publishDate, i.unpublishDate, i.uploadDate, i.lastUpdatedDate, i.lastUpdatedBy, i.highresCntId, i.sortRank, i.privateYesNo,ast.assetSelectionKey, i.internalOnly, i.recordSource ), filteredRawAssets as ( select ra.* from rawAssets ra where (1=1 or ra.linked_brokerIds in (?) ) ) , assetsWithContentURL1 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL, c.fileName as docFileName, lub.firstname + ' ' + lub.lastname as lastUpdatedByFullName from filteredRawAssets ra LEFT OUTER JOIN tbl_FSPRO_Members lub with (nolock) ON ra.updateUserId = lub.fspro_userId inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) , assetsWithContentURL2 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as thumbnailURL, c.fileName as thnFileName from assetsWithContentURL1 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.thumbnailId and c.contentId != 755 ) , assetsWithContentURL3 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as highresURL, c.fileName as highresCntFileName from assetsWithContentURL2 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.highresCntId and c.contentId != 755 ) , assets as ( select a.*, case when right( a.contentURL, charindex( '.', reverse( a.contentURL )) - 1 ) in ( 'BMP','GIF','JPEG','JPG','PNG','TIF','TIFF','WBMP' ) then left( a.contentURL, len( a.contentURL ) - charindex( '.', reverse( a.contentURL ) )) + '_50' + right( a.contentURL, charindex( '.', reverse( a.contentURL )) ) else null end previewURL from assetsWithContentURL3 a ) , categories as ( select 'OPR' contentDomain, categoryId, categoryDescription from tbl_OPR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'CDR' contentDomain, categoryId, categoryDescription from tbl_CDR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'BPL' contentDomain, categoryId, categoryDescription from tbl_BPL_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'DOC' contentDomain, categoryId, categoryName as categoryDescription from tbl_DOC_Categories with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType ) , assetsWithCategory as ( select a.*, c.categoryDescription from assets a left outer join categories c on c.contentDomain = a.contentDomain and c.categoryId = a.categoryId ) ,industryCategories as ( SELECT categoryId, categoryDescription FROM tbl_PRD_RepositoryCategories with (nolock) WHERE containerType = 'SKU' AND ownerID = 0 AND ownerType = '*' AND left(categoryDescription, 3) <> 'GS1' ) , assetsWithIndustryCategory as ( select a.*, ic.categoryDescription as industryCategoryDescription from assetsWithCategory a left outer join industryCategories ic on ic.categoryId = a.categoryId ) , assetsWithPartner as ( select a.*, opr.companyName as partnerName, a.containerId as partnerId, a.containerType as partnerType, opr.territoryId from assetsWithIndustryCategory a inner join tbl_OPR_CLientOperators opr with (nolock) on opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = a.containerId where a.containerType = 'OPR' union all select a.*, cdr.cdr_dstName as partnerName, a.containerId as partnerId, a.containerType as partnerType, cdr.cdr_territoryId as territoryId from assetsWithIndustryCategory a inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = @ownerId and cdr.fsl_tablecode = @ownerType and cdr.cdr_recordId = a.containerId where a.containerType = 'CDR' union all select a.*, mfr.mfr_name as partnerName, mfr.mfr_id as partnerId, 'MFR' as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a inner join tbl_BRO_Principals bpl with (nolock) on bpl.ownerId = @ownerId and bpl.ownerType = @ownerType and bpl.principalId = a.containerId inner join tblManufacturers mfr with (nolock) on mfr.mfr_id = bpl.mfrId where a.containerType = 'BPL' union all select a.*,cast (null as varchar ) as partnerName, cast( null as integer ) as partnerId, cast( null as varchar ) as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a where coalesce( a.containerType, '???') not in ( 'BPL', 'OPR', 'CDR' ) ), assetsWithUser as ( select a.*, m.email updateUserEmail, m.firstName + ' ' + m.lastName as updateUserFullName, case when len(isnull(a.linked_prd,'')) > 0 or len(isnull(a.linked_sku,'')) > 0 then 'Y' else 'N' end as prdLink, case when len(isnull(a.linked_opr,'')) > 0 then 'Y' else 'N' end as oprLink, case when len(isnull(a.linked_cdr,'')) > 0 then 'Y' else 'N' end as cdrLink, case when len(isnull(a.linked_mfr,'')) > 0 then 'Y' else 'N' end as mfrLink from assetsWithPartner as a left outer join tbl_Fspro_members m with (nolock) on m.ownerId > 0 and m.fspro_userId = a.updateUserId ) , assetsWithUser_mappedData as ( select * from assetsWithUser ) , assetsWithUser_preData as ( select * from assetsWithUser_mappedData with (nolock) where ( ( 1 = 1 ) and ( ( ( [isPublished] = 'Y' ) or ( [isPublished] = 'F' ) ) ) ) ) , assetsWithUser_data as ( select * from assetsWithUser_preData ) , assetsWithUser_return as ( select * from assetsWithUser_data where 1 = 1 order by docTitle offset 0 rows fetch next 50 rows only ) , assetsWithUser_result as ( select ( select count(*) from assetsWithUser_data ) as totalCount, ( select * from assetsWithUser_return for json auto, include_null_values ) as data ) select * from assetsWithUser_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER -- 2026-04-16 19:49:18.253 -- 0 {ts '2026-04-16 19:49:21'} SQL declare @ownerId int = 207 ; declare @ownerType varchar(3) = 'MFR' ; declare @contentPrefix varchar(128) = 'http://dev.fsenablers.com/FSLContent/' ; declare @todayDate datetime = ?; with docCategories as ( SELECT cats.attrID as categoryId, cats.attrDescription AS categoryName FROM tbl_ORG_Attributes cats with (nolock) WHERE cats.OwnerID = @ownerId AND cats.FSL_TableCode = @ownerType AND cats.attrTypeID = 31 ), rawAssets as ( select null contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, cast( null as varchar ) docTitle, cast( null as int) as categoryId, cast( null as int ) contentId, cast( null as int) fileId, 0.00 fileSize, null fileExt, null docDescription, cast( null as datetime ) publishDate, cast( null as datetime ) unpublishDate, 'N' isPublished, cast( null as datetime ) uploadDate, cast( null as datetime ) updateDate, cast( null as int ) updateUserId, 0 restricted, -1 thumbnailId, -1 highresCntId, 99 as sortRank , 0 as freshness , 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr, cast( null as varchar ) as assetSelectionKey, 0 as selectedRecord , 'N' as internalOnly, '' as recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds union all select 'DOC' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, d.docTitle, d.repCategoryId as categoryId, d.contentId, d.docId as fileId, CAST((d.fileSize/1024) AS float) as fileSize , d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, case when d.publishDate is not null and d.publishDate > @todayDate then 'F' when d.publishDate is not null and d.unpublishDate is not null and @todayDate >= d.publishDate and @todayDate <= d.unpublishDate then 'Y' when d.publishDate is not null and d.unpublishDate is null and d.publishDate <= @todayDate then 'Y' when d.publishDate is null then 'N' else 'N' end isPublished, d.uploadDate, d.lastUpdatedDate updateDate, d.lastUpdatedBy updateUserId, 0 restricted, thumbnailId, -1 highresCntId, sortRank , coalesce(dateDiff(day, d.lastUpdatedDate, @todayDate),0) as freshness, case when d.privateYesNo = 1 then 'Y' else 'N' end privateYesNo, ou.firstname + ' ' + ou.lastname as uploadedByFullName, d.keyWords, d.unpublishToDo, d.internalNotes , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END), ',') AS linked_opr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END), ',') AS linked_prd , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END), ',') AS linked_sku , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END), ',') AS linked_cdr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END), ',') AS linked_mfr , cast(concat('DOC',':',d.docId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , d.internalOnly, d.recordSource , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 23 THEN li.child_attrID ELSE NULL END), ',') AS permissionGrpIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 7 THEN li.child_attrID ELSE NULL END), ',') AS linked_brokerIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 31 THEN li.child_attrID ELSE NULL END), ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = d.docId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 34 THEN li.child_attrID ELSE NULL END), ',') AS linked_UserMemberIds from tbl_DOC_Documents d with (nolock) LEFT OUTER JOIN tbl_FSPRO_Members ou with (nolock) ON d.AuthorUserId = ou.fspro_userId left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = d.docId and li.child_attrTypeID in (41,59,60,63,42,23,7,31,34) and li.mstr_attrTypeID = 30 left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('DOC',':',d.docId) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType GROUP BY d.docTitle, d.repCategoryId, d.contentId, d.docId,d.fileSize, d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, d.uploadDate, d.lastUpdatedDate, d.lastUpdatedBy,thumbnailId, sortRank , d.privateYesNo , ou.firstname,ou.lastname, d.keyWords, d.unpublishToDo, d.internalNotes,ast.assetSelectionKey, d.internalOnly, d.recordSource union all select 'IMG' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, i.imgTitle as docTitle, i.categoryId, i.contentId, i.imgId as fileId, CAST((i.fileSize/1024) AS float) as fileSize, i.fileExt, i.imgDescription as docDescription, i.publishDate, i.unpublishDate, case when i.publishDate is not null and i.publishDate > @todayDate then 'F' when i.publishDate is not null and i.unpublishDate is not null and @todayDate >= i.publishDate and @todayDate <= i.unpublishDate then 'Y' when i.publishDate is not null and i.unpublishDate is null and i.publishDate <= @todayDate then 'Y' when i.publishDate is null then 'N' else 'N' end isPublished, i.uploadDate, i.lastUpdatedDate updateDate, i.lastUpdatedBy updateUserId, 0 restricted, -1 thumbnailId, i.highresCntId, i.sortRank, coalesce(dateDiff(day, lastUpdatedDate, @todayDate),0) as freshness, case when i.privateYesNo = 1 then 'Y' else 'N' end privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , STRING_AGG(CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END, ',') AS linked_opr , STRING_AGG(CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END, ',') AS linked_prd , STRING_AGG(CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END, ',') AS linked_sku , STRING_AGG(CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END, ',') AS linked_cdr , STRING_AGG(CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END, ',') AS linked_mfr , cast(concat('IMG',':',i.imgId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , i.internalOnly, i.recordSource , STRING_AGG(CASE WHEN c.child_attrTypeID = 23 THEN c.child_attrID ELSE NULL END, ',') AS permissionGrpIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 7 THEN c.child_attrID ELSE NULL END, ',') AS linked_brokerIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 31 THEN c.child_attrID ELSE NULL END, ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = i.imgId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CASE WHEN c.child_attrTypeID = 34 THEN c.child_attrID ELSE NULL END, ',') AS linked_UserMemberIds from tbl_IMG_Images i with (nolock) left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = i.imgId and li.child_attrTypeID in (41,59,60,63,42) and li.mstr_attrTypeID = 33 left outer JOIN tbl_ORG_AttributeLinks c ON c.mstr_attrID = i.imgId and c.child_attrTypeID in (23,7,31,34) and c.mstr_attrTypeID = 30 left outer join tbl_DOC_Categories dc with (nolock) ON c.child_attrTypeID = 31 AND dc.categoryId = c.child_attrID AND dc.ownerId = @ownerId AND dc.fsl_tablecode = @ownerType left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('IMG',':',i.imgId) where i.ownerId = @ownerId and i.fsl_tablecode = @ownerType GROUP BY i.imgTitle, i.CategoryID, i.contentId, i.imgId, i.fileSize, i.fileExt, i.imgDescription, i.publishDate, i.unpublishDate, i.uploadDate, i.lastUpdatedDate, i.lastUpdatedBy, i.highresCntId, i.sortRank, i.privateYesNo,ast.assetSelectionKey, i.internalOnly, i.recordSource ), filteredRawAssets as ( select ra.* from rawAssets ra where (1=1 or ra.linked_brokerIds in (?) ) ) , assetsWithContentURL1 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL, c.fileName as docFileName, lub.firstname + ' ' + lub.lastname as lastUpdatedByFullName from filteredRawAssets ra LEFT OUTER JOIN tbl_FSPRO_Members lub with (nolock) ON ra.updateUserId = lub.fspro_userId inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) , assetsWithContentURL2 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as thumbnailURL, c.fileName as thnFileName from assetsWithContentURL1 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.thumbnailId and c.contentId != 755 ) , assetsWithContentURL3 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as highresURL, c.fileName as highresCntFileName from assetsWithContentURL2 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.highresCntId and c.contentId != 755 ) , assets as ( select a.*, case when right( a.contentURL, charindex( '.', reverse( a.contentURL )) - 1 ) in ( 'BMP','GIF','JPEG','JPG','PNG','TIF','TIFF','WBMP' ) then left( a.contentURL, len( a.contentURL ) - charindex( '.', reverse( a.contentURL ) )) + '_50' + right( a.contentURL, charindex( '.', reverse( a.contentURL )) ) else null end previewURL from assetsWithContentURL3 a ) , categories as ( select 'OPR' contentDomain, categoryId, categoryDescription from tbl_OPR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'CDR' contentDomain, categoryId, categoryDescription from tbl_CDR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'BPL' contentDomain, categoryId, categoryDescription from tbl_BPL_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'DOC' contentDomain, categoryId, categoryName as categoryDescription from tbl_DOC_Categories with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType ) , assetsWithCategory as ( select a.*, c.categoryDescription from assets a left outer join categories c on c.contentDomain = a.contentDomain and c.categoryId = a.categoryId ) ,industryCategories as ( SELECT categoryId, categoryDescription FROM tbl_PRD_RepositoryCategories with (nolock) WHERE containerType = 'SKU' AND ownerID = 0 AND ownerType = '*' AND left(categoryDescription, 3) <> 'GS1' ) , assetsWithIndustryCategory as ( select a.*, ic.categoryDescription as industryCategoryDescription from assetsWithCategory a left outer join industryCategories ic on ic.categoryId = a.categoryId ) , assetsWithPartner as ( select a.*, opr.companyName as partnerName, a.containerId as partnerId, a.containerType as partnerType, opr.territoryId from assetsWithIndustryCategory a inner join tbl_OPR_CLientOperators opr with (nolock) on opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = a.containerId where a.containerType = 'OPR' union all select a.*, cdr.cdr_dstName as partnerName, a.containerId as partnerId, a.containerType as partnerType, cdr.cdr_territoryId as territoryId from assetsWithIndustryCategory a inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = @ownerId and cdr.fsl_tablecode = @ownerType and cdr.cdr_recordId = a.containerId where a.containerType = 'CDR' union all select a.*, mfr.mfr_name as partnerName, mfr.mfr_id as partnerId, 'MFR' as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a inner join tbl_BRO_Principals bpl with (nolock) on bpl.ownerId = @ownerId and bpl.ownerType = @ownerType and bpl.principalId = a.containerId inner join tblManufacturers mfr with (nolock) on mfr.mfr_id = bpl.mfrId where a.containerType = 'BPL' union all select a.*,cast (null as varchar ) as partnerName, cast( null as integer ) as partnerId, cast( null as varchar ) as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a where coalesce( a.containerType, '???') not in ( 'BPL', 'OPR', 'CDR' ) ), assetsWithUser as ( select a.*, m.email updateUserEmail, m.firstName + ' ' + m.lastName as updateUserFullName, case when len(isnull(a.linked_prd,'')) > 0 or len(isnull(a.linked_sku,'')) > 0 then 'Y' else 'N' end as prdLink, case when len(isnull(a.linked_opr,'')) > 0 then 'Y' else 'N' end as oprLink, case when len(isnull(a.linked_cdr,'')) > 0 then 'Y' else 'N' end as cdrLink, case when len(isnull(a.linked_mfr,'')) > 0 then 'Y' else 'N' end as mfrLink from assetsWithPartner as a left outer join tbl_Fspro_members m with (nolock) on m.ownerId > 0 and m.fspro_userId = a.updateUserId ) , assetsWithUser_mappedData as ( select * from assetsWithUser ) , assetsWithUser_preData as ( select * from assetsWithUser_mappedData with (nolock) where ( ( 1 = 1 ) and ( ( ( [isPublished] = 'Y' ) or ( [isPublished] = 'F' ) ) ) ) ) , assetsWithUser_data as ( select * from assetsWithUser_preData ) , assetsWithUser_return as ( select * from assetsWithUser_data where 1 = 1 order by docTitle offset 0 rows fetch next 50 rows only ) , assetsWithUser_result as ( select ( select count(*) from assetsWithUser_data ) as totalCount, ( select * from assetsWithUser_return for json auto, include_null_values ) as data ) select * from assetsWithUser_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER -- 2026-04-16 19:49:21.07 -- 0 {ts '2026-04-16 19:49:21'} SQL declare @ownerId int = 207 ; declare @ownerType varchar(3) = 'MFR' ; declare @contentPrefix varchar(128) = 'http://dev.fsenablers.com/FSLContent/' ; declare @todayDate datetime = ?; with docCategories as ( SELECT cats.attrID as categoryId, cats.attrDescription AS categoryName FROM tbl_ORG_Attributes cats with (nolock) WHERE cats.OwnerID = @ownerId AND cats.FSL_TableCode = @ownerType AND cats.attrTypeID = 31 ), rawAssets as ( select null contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, cast( null as varchar ) docTitle, cast( null as int) as categoryId, cast( null as int ) contentId, cast( null as int) fileId, 0.00 fileSize, null fileExt, null docDescription, cast( null as datetime ) publishDate, cast( null as datetime ) unpublishDate, 'N' isPublished, cast( null as datetime ) uploadDate, cast( null as datetime ) updateDate, cast( null as int ) updateUserId, 0 restricted, -1 thumbnailId, -1 highresCntId, 99 as sortRank , 0 as freshness , 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr, cast( null as varchar ) as assetSelectionKey, 0 as selectedRecord , 'N' as internalOnly, '' as recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds union all select 'DOC' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, d.docTitle, d.repCategoryId as categoryId, d.contentId, d.docId as fileId, CAST((d.fileSize/1024) AS float) as fileSize , d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, case when d.publishDate is not null and d.publishDate > @todayDate then 'F' when d.publishDate is not null and d.unpublishDate is not null and @todayDate >= d.publishDate and @todayDate <= d.unpublishDate then 'Y' when d.publishDate is not null and d.unpublishDate is null and d.publishDate <= @todayDate then 'Y' when d.publishDate is null then 'N' else 'N' end isPublished, d.uploadDate, d.lastUpdatedDate updateDate, d.lastUpdatedBy updateUserId, 0 restricted, thumbnailId, -1 highresCntId, sortRank , coalesce(dateDiff(day, d.lastUpdatedDate, @todayDate),0) as freshness, case when d.privateYesNo = 1 then 'Y' else 'N' end privateYesNo, ou.firstname + ' ' + ou.lastname as uploadedByFullName, d.keyWords, d.unpublishToDo, d.internalNotes , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END), ',') AS linked_opr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END), ',') AS linked_prd , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END), ',') AS linked_sku , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END), ',') AS linked_cdr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END), ',') AS linked_mfr , cast(concat('DOC',':',d.docId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , d.internalOnly, d.recordSource , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 23 THEN li.child_attrID ELSE NULL END), ',') AS permissionGrpIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 7 THEN li.child_attrID ELSE NULL END), ',') AS linked_brokerIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 31 THEN li.child_attrID ELSE NULL END), ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = d.docId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 34 THEN li.child_attrID ELSE NULL END), ',') AS linked_UserMemberIds from tbl_DOC_Documents d with (nolock) LEFT OUTER JOIN tbl_FSPRO_Members ou with (nolock) ON d.AuthorUserId = ou.fspro_userId left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = d.docId and li.child_attrTypeID in (41,59,60,63,42,23,7,31,34) and li.mstr_attrTypeID = 30 left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('DOC',':',d.docId) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType GROUP BY d.docTitle, d.repCategoryId, d.contentId, d.docId,d.fileSize, d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, d.uploadDate, d.lastUpdatedDate, d.lastUpdatedBy,thumbnailId, sortRank , d.privateYesNo , ou.firstname,ou.lastname, d.keyWords, d.unpublishToDo, d.internalNotes,ast.assetSelectionKey, d.internalOnly, d.recordSource union all select 'IMG' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, i.imgTitle as docTitle, i.categoryId, i.contentId, i.imgId as fileId, CAST((i.fileSize/1024) AS float) as fileSize, i.fileExt, i.imgDescription as docDescription, i.publishDate, i.unpublishDate, case when i.publishDate is not null and i.publishDate > @todayDate then 'F' when i.publishDate is not null and i.unpublishDate is not null and @todayDate >= i.publishDate and @todayDate <= i.unpublishDate then 'Y' when i.publishDate is not null and i.unpublishDate is null and i.publishDate <= @todayDate then 'Y' when i.publishDate is null then 'N' else 'N' end isPublished, i.uploadDate, i.lastUpdatedDate updateDate, i.lastUpdatedBy updateUserId, 0 restricted, -1 thumbnailId, i.highresCntId, i.sortRank, coalesce(dateDiff(day, lastUpdatedDate, @todayDate),0) as freshness, case when i.privateYesNo = 1 then 'Y' else 'N' end privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , STRING_AGG(CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END, ',') AS linked_opr , STRING_AGG(CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END, ',') AS linked_prd , STRING_AGG(CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END, ',') AS linked_sku , STRING_AGG(CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END, ',') AS linked_cdr , STRING_AGG(CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END, ',') AS linked_mfr , cast(concat('IMG',':',i.imgId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , i.internalOnly, i.recordSource , STRING_AGG(CASE WHEN c.child_attrTypeID = 23 THEN c.child_attrID ELSE NULL END, ',') AS permissionGrpIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 7 THEN c.child_attrID ELSE NULL END, ',') AS linked_brokerIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 31 THEN c.child_attrID ELSE NULL END, ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = i.imgId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CASE WHEN c.child_attrTypeID = 34 THEN c.child_attrID ELSE NULL END, ',') AS linked_UserMemberIds from tbl_IMG_Images i with (nolock) left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = i.imgId and li.child_attrTypeID in (41,59,60,63,42) and li.mstr_attrTypeID = 33 left outer JOIN tbl_ORG_AttributeLinks c ON c.mstr_attrID = i.imgId and c.child_attrTypeID in (23,7,31,34) and c.mstr_attrTypeID = 30 left outer join tbl_DOC_Categories dc with (nolock) ON c.child_attrTypeID = 31 AND dc.categoryId = c.child_attrID AND dc.ownerId = @ownerId AND dc.fsl_tablecode = @ownerType left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('IMG',':',i.imgId) where i.ownerId = @ownerId and i.fsl_tablecode = @ownerType GROUP BY i.imgTitle, i.CategoryID, i.contentId, i.imgId, i.fileSize, i.fileExt, i.imgDescription, i.publishDate, i.unpublishDate, i.uploadDate, i.lastUpdatedDate, i.lastUpdatedBy, i.highresCntId, i.sortRank, i.privateYesNo,ast.assetSelectionKey, i.internalOnly, i.recordSource ), filteredRawAssets as ( select ra.* from rawAssets ra where (1=1 or ra.linked_brokerIds in (?) ) ) , assetsWithContentURL1 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL, c.fileName as docFileName, lub.firstname + ' ' + lub.lastname as lastUpdatedByFullName from filteredRawAssets ra LEFT OUTER JOIN tbl_FSPRO_Members lub with (nolock) ON ra.updateUserId = lub.fspro_userId inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) , assetsWithContentURL2 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as thumbnailURL, c.fileName as thnFileName from assetsWithContentURL1 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.thumbnailId and c.contentId != 755 ) , assetsWithContentURL3 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as highresURL, c.fileName as highresCntFileName from assetsWithContentURL2 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.highresCntId and c.contentId != 755 ) , assets as ( select a.*, case when right( a.contentURL, charindex( '.', reverse( a.contentURL )) - 1 ) in ( 'BMP','GIF','JPEG','JPG','PNG','TIF','TIFF','WBMP' ) then left( a.contentURL, len( a.contentURL ) - charindex( '.', reverse( a.contentURL ) )) + '_50' + right( a.contentURL, charindex( '.', reverse( a.contentURL )) ) else null end previewURL from assetsWithContentURL3 a ) , categories as ( select 'OPR' contentDomain, categoryId, categoryDescription from tbl_OPR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'CDR' contentDomain, categoryId, categoryDescription from tbl_CDR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'BPL' contentDomain, categoryId, categoryDescription from tbl_BPL_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'DOC' contentDomain, categoryId, categoryName as categoryDescription from tbl_DOC_Categories with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType ) , assetsWithCategory as ( select a.*, c.categoryDescription from assets a left outer join categories c on c.contentDomain = a.contentDomain and c.categoryId = a.categoryId ) ,industryCategories as ( SELECT categoryId, categoryDescription FROM tbl_PRD_RepositoryCategories with (nolock) WHERE containerType = 'SKU' AND ownerID = 0 AND ownerType = '*' AND left(categoryDescription, 3) <> 'GS1' ) , assetsWithIndustryCategory as ( select a.*, ic.categoryDescription as industryCategoryDescription from assetsWithCategory a left outer join industryCategories ic on ic.categoryId = a.categoryId ) , assetsWithPartner as ( select a.*, opr.companyName as partnerName, a.containerId as partnerId, a.containerType as partnerType, opr.territoryId from assetsWithIndustryCategory a inner join tbl_OPR_CLientOperators opr with (nolock) on opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = a.containerId where a.containerType = 'OPR' union all select a.*, cdr.cdr_dstName as partnerName, a.containerId as partnerId, a.containerType as partnerType, cdr.cdr_territoryId as territoryId from assetsWithIndustryCategory a inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = @ownerId and cdr.fsl_tablecode = @ownerType and cdr.cdr_recordId = a.containerId where a.containerType = 'CDR' union all select a.*, mfr.mfr_name as partnerName, mfr.mfr_id as partnerId, 'MFR' as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a inner join tbl_BRO_Principals bpl with (nolock) on bpl.ownerId = @ownerId and bpl.ownerType = @ownerType and bpl.principalId = a.containerId inner join tblManufacturers mfr with (nolock) on mfr.mfr_id = bpl.mfrId where a.containerType = 'BPL' union all select a.*,cast (null as varchar ) as partnerName, cast( null as integer ) as partnerId, cast( null as varchar ) as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a where coalesce( a.containerType, '???') not in ( 'BPL', 'OPR', 'CDR' ) ), assetsWithUser as ( select a.*, m.email updateUserEmail, m.firstName + ' ' + m.lastName as updateUserFullName, case when len(isnull(a.linked_prd,'')) > 0 or len(isnull(a.linked_sku,'')) > 0 then 'Y' else 'N' end as prdLink, case when len(isnull(a.linked_opr,'')) > 0 then 'Y' else 'N' end as oprLink, case when len(isnull(a.linked_cdr,'')) > 0 then 'Y' else 'N' end as cdrLink, case when len(isnull(a.linked_mfr,'')) > 0 then 'Y' else 'N' end as mfrLink from assetsWithPartner as a left outer join tbl_Fspro_members m with (nolock) on m.ownerId > 0 and m.fspro_userId = a.updateUserId ) , assetsWithUser_mappedData as ( select * from assetsWithUser ) , assetsWithUser_preData as ( select * from assetsWithUser_mappedData with (nolock) where ( ( 1 = 1 ) and ( ( ( [isPublished] = 'Y' ) or ( [isPublished] = 'F' ) ) ) ) ) , assetsWithUser_data as ( select * from assetsWithUser_preData ) , assetsWithUser_return as ( select * from assetsWithUser_data where 1 = 1 order by docTitle offset 0 rows fetch next 50 rows only ) , assetsWithUser_result as ( select ( select count(*) from assetsWithUser_data ) as totalCount, ( select * from assetsWithUser_return for json auto, include_null_values ) as data ) select * from assetsWithUser_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER -- 2026-04-16 19:49:21.121 -- 0 {ts '2026-04-16 23:28:10'} SQL declare @ownerId int = 207 ; declare @ownerType varchar(3) = 'MFR' ; declare @contentPrefix varchar(128) = 'http://dev.fsenablers.com/FSLContent/' ; declare @todayDate datetime = ?; with docCategories as ( SELECT cats.attrID as categoryId, cats.attrDescription AS categoryName FROM tbl_ORG_Attributes cats with (nolock) WHERE cats.OwnerID = @ownerId AND cats.FSL_TableCode = @ownerType AND cats.attrTypeID = 31 ), rawAssets as ( select null contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, cast( null as varchar ) docTitle, cast( null as int) as categoryId, cast( null as int ) contentId, cast( null as int) fileId, 0.00 fileSize, null fileExt, null docDescription, cast( null as datetime ) publishDate, cast( null as datetime ) unpublishDate, 'N' isPublished, cast( null as datetime ) uploadDate, cast( null as datetime ) updateDate, cast( null as int ) updateUserId, 0 restricted, -1 thumbnailId, -1 highresCntId, 99 as sortRank , 0 as freshness , 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr, cast( null as varchar ) as assetSelectionKey, 0 as selectedRecord , 'N' as internalOnly, '' as recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds union all select 'DOC' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, d.docTitle, d.repCategoryId as categoryId, d.contentId, d.docId as fileId, CAST((d.fileSize/1024) AS float) as fileSize , d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, case when d.publishDate is not null and d.publishDate > @todayDate then 'F' when d.publishDate is not null and d.unpublishDate is not null and @todayDate >= d.publishDate and @todayDate <= d.unpublishDate then 'Y' when d.publishDate is not null and d.unpublishDate is null and d.publishDate <= @todayDate then 'Y' when d.publishDate is null then 'N' else 'N' end isPublished, d.uploadDate, d.lastUpdatedDate updateDate, d.lastUpdatedBy updateUserId, 0 restricted, thumbnailId, -1 highresCntId, sortRank , coalesce(dateDiff(day, d.lastUpdatedDate, @todayDate),0) as freshness, case when d.privateYesNo = 1 then 'Y' else 'N' end privateYesNo, ou.firstname + ' ' + ou.lastname as uploadedByFullName, d.keyWords, d.unpublishToDo, d.internalNotes , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END), ',') AS linked_opr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END), ',') AS linked_prd , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END), ',') AS linked_sku , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END), ',') AS linked_cdr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END), ',') AS linked_mfr , cast(concat('DOC',':',d.docId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , d.internalOnly, d.recordSource , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 23 THEN li.child_attrID ELSE NULL END), ',') AS permissionGrpIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 7 THEN li.child_attrID ELSE NULL END), ',') AS linked_brokerIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 31 THEN li.child_attrID ELSE NULL END), ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = d.docId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 34 THEN li.child_attrID ELSE NULL END), ',') AS linked_UserMemberIds from tbl_DOC_Documents d with (nolock) LEFT OUTER JOIN tbl_FSPRO_Members ou with (nolock) ON d.AuthorUserId = ou.fspro_userId left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = d.docId and li.child_attrTypeID in (41,59,60,63,42,23,7,31,34) and li.mstr_attrTypeID = 30 left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('DOC',':',d.docId) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType GROUP BY d.docTitle, d.repCategoryId, d.contentId, d.docId,d.fileSize, d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, d.uploadDate, d.lastUpdatedDate, d.lastUpdatedBy,thumbnailId, sortRank , d.privateYesNo , ou.firstname,ou.lastname, d.keyWords, d.unpublishToDo, d.internalNotes,ast.assetSelectionKey, d.internalOnly, d.recordSource union all select 'IMG' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, i.imgTitle as docTitle, i.categoryId, i.contentId, i.imgId as fileId, CAST((i.fileSize/1024) AS float) as fileSize, i.fileExt, i.imgDescription as docDescription, i.publishDate, i.unpublishDate, case when i.publishDate is not null and i.publishDate > @todayDate then 'F' when i.publishDate is not null and i.unpublishDate is not null and @todayDate >= i.publishDate and @todayDate <= i.unpublishDate then 'Y' when i.publishDate is not null and i.unpublishDate is null and i.publishDate <= @todayDate then 'Y' when i.publishDate is null then 'N' else 'N' end isPublished, i.uploadDate, i.lastUpdatedDate updateDate, i.lastUpdatedBy updateUserId, 0 restricted, -1 thumbnailId, i.highresCntId, i.sortRank, coalesce(dateDiff(day, lastUpdatedDate, @todayDate),0) as freshness, case when i.privateYesNo = 1 then 'Y' else 'N' end privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , STRING_AGG(CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END, ',') AS linked_opr , STRING_AGG(CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END, ',') AS linked_prd , STRING_AGG(CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END, ',') AS linked_sku , STRING_AGG(CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END, ',') AS linked_cdr , STRING_AGG(CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END, ',') AS linked_mfr , cast(concat('IMG',':',i.imgId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , i.internalOnly, i.recordSource , STRING_AGG(CASE WHEN c.child_attrTypeID = 23 THEN c.child_attrID ELSE NULL END, ',') AS permissionGrpIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 7 THEN c.child_attrID ELSE NULL END, ',') AS linked_brokerIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 31 THEN c.child_attrID ELSE NULL END, ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = i.imgId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CASE WHEN c.child_attrTypeID = 34 THEN c.child_attrID ELSE NULL END, ',') AS linked_UserMemberIds from tbl_IMG_Images i with (nolock) left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = i.imgId and li.child_attrTypeID in (41,59,60,63,42) and li.mstr_attrTypeID = 33 left outer JOIN tbl_ORG_AttributeLinks c ON c.mstr_attrID = i.imgId and c.child_attrTypeID in (23,7,31,34) and c.mstr_attrTypeID = 30 left outer join tbl_DOC_Categories dc with (nolock) ON c.child_attrTypeID = 31 AND dc.categoryId = c.child_attrID AND dc.ownerId = @ownerId AND dc.fsl_tablecode = @ownerType left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('IMG',':',i.imgId) where i.ownerId = @ownerId and i.fsl_tablecode = @ownerType GROUP BY i.imgTitle, i.CategoryID, i.contentId, i.imgId, i.fileSize, i.fileExt, i.imgDescription, i.publishDate, i.unpublishDate, i.uploadDate, i.lastUpdatedDate, i.lastUpdatedBy, i.highresCntId, i.sortRank, i.privateYesNo,ast.assetSelectionKey, i.internalOnly, i.recordSource ), filteredRawAssets as ( select ra.* from rawAssets ra where (1=1 or ra.linked_brokerIds in (?) ) ) , assetsWithContentURL1 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL, c.fileName as docFileName, lub.firstname + ' ' + lub.lastname as lastUpdatedByFullName from filteredRawAssets ra LEFT OUTER JOIN tbl_FSPRO_Members lub with (nolock) ON ra.updateUserId = lub.fspro_userId inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) , assetsWithContentURL2 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as thumbnailURL, c.fileName as thnFileName from assetsWithContentURL1 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.thumbnailId and c.contentId != 755 ) , assetsWithContentURL3 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as highresURL, c.fileName as highresCntFileName from assetsWithContentURL2 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.highresCntId and c.contentId != 755 ) , assets as ( select a.*, case when right( a.contentURL, charindex( '.', reverse( a.contentURL )) - 1 ) in ( 'BMP','GIF','JPEG','JPG','PNG','TIF','TIFF','WBMP' ) then left( a.contentURL, len( a.contentURL ) - charindex( '.', reverse( a.contentURL ) )) + '_50' + right( a.contentURL, charindex( '.', reverse( a.contentURL )) ) else null end previewURL from assetsWithContentURL3 a ) , categories as ( select 'OPR' contentDomain, categoryId, categoryDescription from tbl_OPR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'CDR' contentDomain, categoryId, categoryDescription from tbl_CDR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'BPL' contentDomain, categoryId, categoryDescription from tbl_BPL_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'DOC' contentDomain, categoryId, categoryName as categoryDescription from tbl_DOC_Categories with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType ) , assetsWithCategory as ( select a.*, c.categoryDescription from assets a left outer join categories c on c.contentDomain = a.contentDomain and c.categoryId = a.categoryId ) ,industryCategories as ( SELECT categoryId, categoryDescription FROM tbl_PRD_RepositoryCategories with (nolock) WHERE containerType = 'SKU' AND ownerID = 0 AND ownerType = '*' AND left(categoryDescription, 3) <> 'GS1' ) , assetsWithIndustryCategory as ( select a.*, ic.categoryDescription as industryCategoryDescription from assetsWithCategory a left outer join industryCategories ic on ic.categoryId = a.categoryId ) , assetsWithPartner as ( select a.*, opr.companyName as partnerName, a.containerId as partnerId, a.containerType as partnerType, opr.territoryId from assetsWithIndustryCategory a inner join tbl_OPR_CLientOperators opr with (nolock) on opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = a.containerId where a.containerType = 'OPR' union all select a.*, cdr.cdr_dstName as partnerName, a.containerId as partnerId, a.containerType as partnerType, cdr.cdr_territoryId as territoryId from assetsWithIndustryCategory a inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = @ownerId and cdr.fsl_tablecode = @ownerType and cdr.cdr_recordId = a.containerId where a.containerType = 'CDR' union all select a.*, mfr.mfr_name as partnerName, mfr.mfr_id as partnerId, 'MFR' as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a inner join tbl_BRO_Principals bpl with (nolock) on bpl.ownerId = @ownerId and bpl.ownerType = @ownerType and bpl.principalId = a.containerId inner join tblManufacturers mfr with (nolock) on mfr.mfr_id = bpl.mfrId where a.containerType = 'BPL' union all select a.*,cast (null as varchar ) as partnerName, cast( null as integer ) as partnerId, cast( null as varchar ) as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a where coalesce( a.containerType, '???') not in ( 'BPL', 'OPR', 'CDR' ) ), assetsWithUser as ( select a.*, m.email updateUserEmail, m.firstName + ' ' + m.lastName as updateUserFullName, case when len(isnull(a.linked_prd,'')) > 0 or len(isnull(a.linked_sku,'')) > 0 then 'Y' else 'N' end as prdLink, case when len(isnull(a.linked_opr,'')) > 0 then 'Y' else 'N' end as oprLink, case when len(isnull(a.linked_cdr,'')) > 0 then 'Y' else 'N' end as cdrLink, case when len(isnull(a.linked_mfr,'')) > 0 then 'Y' else 'N' end as mfrLink from assetsWithPartner as a left outer join tbl_Fspro_members m with (nolock) on m.ownerId > 0 and m.fspro_userId = a.updateUserId ) , assetsWithUser_mappedData as ( select * from assetsWithUser ) , assetsWithUser_preData as ( select * from assetsWithUser_mappedData with (nolock) where ( ( 1 = 1 ) and ( ( ( [isPublished] = 'Y' ) or ( [isPublished] = 'F' ) ) ) ) ) , assetsWithUser_data as ( select * from assetsWithUser_preData ) , assetsWithUser_return as ( select * from assetsWithUser_data where 1 = 1 order by docTitle offset 0 rows fetch next 50 rows only ) , assetsWithUser_result as ( select ( select count(*) from assetsWithUser_data ) as totalCount, ( select * from assetsWithUser_return for json auto, include_null_values ) as data ) select * from assetsWithUser_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER -- 2026-04-16 23:28:10.175 -- 0 {ts '2026-04-16 23:30:19'} SQL declare @ownerId int = 207 ; declare @ownerType varchar(3) = 'MFR' ; declare @contentPrefix varchar(128) = 'http://dev.fsenablers.com/FSLContent/' ; declare @todayDate datetime = ?; with docCategories as ( SELECT cats.attrID as categoryId, cats.attrDescription AS categoryName FROM tbl_ORG_Attributes cats with (nolock) WHERE cats.OwnerID = @ownerId AND cats.FSL_TableCode = @ownerType AND cats.attrTypeID = 31 ), rawAssets as ( select null contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, cast( null as varchar ) docTitle, cast( null as int) as categoryId, cast( null as int ) contentId, cast( null as int) fileId, 0.00 fileSize, null fileExt, null docDescription, cast( null as datetime ) publishDate, cast( null as datetime ) unpublishDate, 'N' isPublished, cast( null as datetime ) uploadDate, cast( null as datetime ) updateDate, cast( null as int ) updateUserId, 0 restricted, -1 thumbnailId, -1 highresCntId, 99 as sortRank , 0 as freshness , 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr, cast( null as varchar ) as assetSelectionKey, 0 as selectedRecord , 'N' as internalOnly, '' as recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds union all select 'DOC' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, d.docTitle, d.repCategoryId as categoryId, d.contentId, d.docId as fileId, CAST((d.fileSize/1024) AS float) as fileSize , d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, case when d.publishDate is not null and d.publishDate > @todayDate then 'F' when d.publishDate is not null and d.unpublishDate is not null and @todayDate >= d.publishDate and @todayDate <= d.unpublishDate then 'Y' when d.publishDate is not null and d.unpublishDate is null and d.publishDate <= @todayDate then 'Y' when d.publishDate is null then 'N' else 'N' end isPublished, d.uploadDate, d.lastUpdatedDate updateDate, d.lastUpdatedBy updateUserId, 0 restricted, thumbnailId, -1 highresCntId, sortRank , coalesce(dateDiff(day, d.lastUpdatedDate, @todayDate),0) as freshness, case when d.privateYesNo = 1 then 'Y' else 'N' end privateYesNo, ou.firstname + ' ' + ou.lastname as uploadedByFullName, d.keyWords, d.unpublishToDo, d.internalNotes , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END), ',') AS linked_opr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END), ',') AS linked_prd , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END), ',') AS linked_sku , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END), ',') AS linked_cdr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END), ',') AS linked_mfr , cast(concat('DOC',':',d.docId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , d.internalOnly, d.recordSource , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 23 THEN li.child_attrID ELSE NULL END), ',') AS permissionGrpIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 7 THEN li.child_attrID ELSE NULL END), ',') AS linked_brokerIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 31 THEN li.child_attrID ELSE NULL END), ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = d.docId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 34 THEN li.child_attrID ELSE NULL END), ',') AS linked_UserMemberIds from tbl_DOC_Documents d with (nolock) LEFT OUTER JOIN tbl_FSPRO_Members ou with (nolock) ON d.AuthorUserId = ou.fspro_userId left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = d.docId and li.child_attrTypeID in (41,59,60,63,42,23,7,31,34) and li.mstr_attrTypeID = 30 left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('DOC',':',d.docId) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType GROUP BY d.docTitle, d.repCategoryId, d.contentId, d.docId,d.fileSize, d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, d.uploadDate, d.lastUpdatedDate, d.lastUpdatedBy,thumbnailId, sortRank , d.privateYesNo , ou.firstname,ou.lastname, d.keyWords, d.unpublishToDo, d.internalNotes,ast.assetSelectionKey, d.internalOnly, d.recordSource union all select 'IMG' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, i.imgTitle as docTitle, i.categoryId, i.contentId, i.imgId as fileId, CAST((i.fileSize/1024) AS float) as fileSize, i.fileExt, i.imgDescription as docDescription, i.publishDate, i.unpublishDate, case when i.publishDate is not null and i.publishDate > @todayDate then 'F' when i.publishDate is not null and i.unpublishDate is not null and @todayDate >= i.publishDate and @todayDate <= i.unpublishDate then 'Y' when i.publishDate is not null and i.unpublishDate is null and i.publishDate <= @todayDate then 'Y' when i.publishDate is null then 'N' else 'N' end isPublished, i.uploadDate, i.lastUpdatedDate updateDate, i.lastUpdatedBy updateUserId, 0 restricted, -1 thumbnailId, i.highresCntId, i.sortRank, coalesce(dateDiff(day, lastUpdatedDate, @todayDate),0) as freshness, case when i.privateYesNo = 1 then 'Y' else 'N' end privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , STRING_AGG(CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END, ',') AS linked_opr , STRING_AGG(CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END, ',') AS linked_prd , STRING_AGG(CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END, ',') AS linked_sku , STRING_AGG(CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END, ',') AS linked_cdr , STRING_AGG(CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END, ',') AS linked_mfr , cast(concat('IMG',':',i.imgId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , i.internalOnly, i.recordSource , STRING_AGG(CASE WHEN c.child_attrTypeID = 23 THEN c.child_attrID ELSE NULL END, ',') AS permissionGrpIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 7 THEN c.child_attrID ELSE NULL END, ',') AS linked_brokerIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 31 THEN c.child_attrID ELSE NULL END, ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = i.imgId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CASE WHEN c.child_attrTypeID = 34 THEN c.child_attrID ELSE NULL END, ',') AS linked_UserMemberIds from tbl_IMG_Images i with (nolock) left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = i.imgId and li.child_attrTypeID in (41,59,60,63,42) and li.mstr_attrTypeID = 33 left outer JOIN tbl_ORG_AttributeLinks c ON c.mstr_attrID = i.imgId and c.child_attrTypeID in (23,7,31,34) and c.mstr_attrTypeID = 30 left outer join tbl_DOC_Categories dc with (nolock) ON c.child_attrTypeID = 31 AND dc.categoryId = c.child_attrID AND dc.ownerId = @ownerId AND dc.fsl_tablecode = @ownerType left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('IMG',':',i.imgId) where i.ownerId = @ownerId and i.fsl_tablecode = @ownerType GROUP BY i.imgTitle, i.CategoryID, i.contentId, i.imgId, i.fileSize, i.fileExt, i.imgDescription, i.publishDate, i.unpublishDate, i.uploadDate, i.lastUpdatedDate, i.lastUpdatedBy, i.highresCntId, i.sortRank, i.privateYesNo,ast.assetSelectionKey, i.internalOnly, i.recordSource ), filteredRawAssets as ( select ra.* from rawAssets ra where (1=1 or ra.linked_brokerIds in (?) ) ) , assetsWithContentURL1 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL, c.fileName as docFileName, lub.firstname + ' ' + lub.lastname as lastUpdatedByFullName from filteredRawAssets ra LEFT OUTER JOIN tbl_FSPRO_Members lub with (nolock) ON ra.updateUserId = lub.fspro_userId inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) , assetsWithContentURL2 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as thumbnailURL, c.fileName as thnFileName from assetsWithContentURL1 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.thumbnailId and c.contentId != 755 ) , assetsWithContentURL3 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as highresURL, c.fileName as highresCntFileName from assetsWithContentURL2 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.highresCntId and c.contentId != 755 ) , assets as ( select a.*, case when right( a.contentURL, charindex( '.', reverse( a.contentURL )) - 1 ) in ( 'BMP','GIF','JPEG','JPG','PNG','TIF','TIFF','WBMP' ) then left( a.contentURL, len( a.contentURL ) - charindex( '.', reverse( a.contentURL ) )) + '_50' + right( a.contentURL, charindex( '.', reverse( a.contentURL )) ) else null end previewURL from assetsWithContentURL3 a ) , categories as ( select 'OPR' contentDomain, categoryId, categoryDescription from tbl_OPR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'CDR' contentDomain, categoryId, categoryDescription from tbl_CDR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'BPL' contentDomain, categoryId, categoryDescription from tbl_BPL_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'DOC' contentDomain, categoryId, categoryName as categoryDescription from tbl_DOC_Categories with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType ) , assetsWithCategory as ( select a.*, c.categoryDescription from assets a left outer join categories c on c.contentDomain = a.contentDomain and c.categoryId = a.categoryId ) ,industryCategories as ( SELECT categoryId, categoryDescription FROM tbl_PRD_RepositoryCategories with (nolock) WHERE containerType = 'SKU' AND ownerID = 0 AND ownerType = '*' AND left(categoryDescription, 3) <> 'GS1' ) , assetsWithIndustryCategory as ( select a.*, ic.categoryDescription as industryCategoryDescription from assetsWithCategory a left outer join industryCategories ic on ic.categoryId = a.categoryId ) , assetsWithPartner as ( select a.*, opr.companyName as partnerName, a.containerId as partnerId, a.containerType as partnerType, opr.territoryId from assetsWithIndustryCategory a inner join tbl_OPR_CLientOperators opr with (nolock) on opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = a.containerId where a.containerType = 'OPR' union all select a.*, cdr.cdr_dstName as partnerName, a.containerId as partnerId, a.containerType as partnerType, cdr.cdr_territoryId as territoryId from assetsWithIndustryCategory a inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = @ownerId and cdr.fsl_tablecode = @ownerType and cdr.cdr_recordId = a.containerId where a.containerType = 'CDR' union all select a.*, mfr.mfr_name as partnerName, mfr.mfr_id as partnerId, 'MFR' as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a inner join tbl_BRO_Principals bpl with (nolock) on bpl.ownerId = @ownerId and bpl.ownerType = @ownerType and bpl.principalId = a.containerId inner join tblManufacturers mfr with (nolock) on mfr.mfr_id = bpl.mfrId where a.containerType = 'BPL' union all select a.*,cast (null as varchar ) as partnerName, cast( null as integer ) as partnerId, cast( null as varchar ) as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a where coalesce( a.containerType, '???') not in ( 'BPL', 'OPR', 'CDR' ) ), assetsWithUser as ( select a.*, m.email updateUserEmail, m.firstName + ' ' + m.lastName as updateUserFullName, case when len(isnull(a.linked_prd,'')) > 0 or len(isnull(a.linked_sku,'')) > 0 then 'Y' else 'N' end as prdLink, case when len(isnull(a.linked_opr,'')) > 0 then 'Y' else 'N' end as oprLink, case when len(isnull(a.linked_cdr,'')) > 0 then 'Y' else 'N' end as cdrLink, case when len(isnull(a.linked_mfr,'')) > 0 then 'Y' else 'N' end as mfrLink from assetsWithPartner as a left outer join tbl_Fspro_members m with (nolock) on m.ownerId > 0 and m.fspro_userId = a.updateUserId ) , assetsWithUser_mappedData as ( select * from assetsWithUser ) , assetsWithUser_preData as ( select * from assetsWithUser_mappedData with (nolock) where ( ( 1 = 1 ) and ( ( ( [isPublished] = 'Y' ) or ( [isPublished] = 'F' ) ) ) ) ) , assetsWithUser_data as ( select * from assetsWithUser_preData ) , assetsWithUser_return as ( select * from assetsWithUser_data where 1 = 1 order by docTitle offset 0 rows fetch next 50 rows only ) , assetsWithUser_result as ( select ( select count(*) from assetsWithUser_data ) as totalCount, ( select * from assetsWithUser_return for json auto, include_null_values ) as data ) select * from assetsWithUser_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER -- 2026-04-16 23:30:19.194 -- 0 {ts '2026-04-16 23:39:47'} SQL declare @ownerId int = 207 ; declare @ownerType varchar(3) = 'MFR' ; declare @contentPrefix varchar(128) = 'http://dev.fsenablers.com/FSLContent/' ; declare @todayDate datetime = ?; with docCategories as ( SELECT cats.attrID as categoryId, cats.attrDescription AS categoryName FROM tbl_ORG_Attributes cats with (nolock) WHERE cats.OwnerID = @ownerId AND cats.FSL_TableCode = @ownerType AND cats.attrTypeID = 31 ), rawAssets as ( select null contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, cast( null as varchar ) docTitle, cast( null as int) as categoryId, cast( null as int ) contentId, cast( null as int) fileId, 0.00 fileSize, null fileExt, null docDescription, cast( null as datetime ) publishDate, cast( null as datetime ) unpublishDate, 'N' isPublished, cast( null as datetime ) uploadDate, cast( null as datetime ) updateDate, cast( null as int ) updateUserId, 0 restricted, -1 thumbnailId, -1 highresCntId, 99 as sortRank , 0 as freshness , 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr, cast( null as varchar ) as assetSelectionKey, 0 as selectedRecord , 'N' as internalOnly, '' as recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds union all select 'DOC' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, d.docTitle, d.repCategoryId as categoryId, d.contentId, d.docId as fileId, CAST((d.fileSize/1024) AS float) as fileSize , d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, case when d.publishDate is not null and d.publishDate > @todayDate then 'F' when d.publishDate is not null and d.unpublishDate is not null and @todayDate >= d.publishDate and @todayDate <= d.unpublishDate then 'Y' when d.publishDate is not null and d.unpublishDate is null and d.publishDate <= @todayDate then 'Y' when d.publishDate is null then 'N' else 'N' end isPublished, d.uploadDate, d.lastUpdatedDate updateDate, d.lastUpdatedBy updateUserId, 0 restricted, thumbnailId, -1 highresCntId, sortRank , coalesce(dateDiff(day, d.lastUpdatedDate, @todayDate),0) as freshness, case when d.privateYesNo = 1 then 'Y' else 'N' end privateYesNo, ou.firstname + ' ' + ou.lastname as uploadedByFullName, d.keyWords, d.unpublishToDo, d.internalNotes , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END), ',') AS linked_opr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END), ',') AS linked_prd , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END), ',') AS linked_sku , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END), ',') AS linked_cdr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END), ',') AS linked_mfr , cast(concat('DOC',':',d.docId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , d.internalOnly, d.recordSource , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 23 THEN li.child_attrID ELSE NULL END), ',') AS permissionGrpIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 7 THEN li.child_attrID ELSE NULL END), ',') AS linked_brokerIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 31 THEN li.child_attrID ELSE NULL END), ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = d.docId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 34 THEN li.child_attrID ELSE NULL END), ',') AS linked_UserMemberIds from tbl_DOC_Documents d with (nolock) LEFT OUTER JOIN tbl_FSPRO_Members ou with (nolock) ON d.AuthorUserId = ou.fspro_userId left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = d.docId and li.child_attrTypeID in (41,59,60,63,42,23,7,31,34) and li.mstr_attrTypeID = 30 left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('DOC',':',d.docId) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType GROUP BY d.docTitle, d.repCategoryId, d.contentId, d.docId,d.fileSize, d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, d.uploadDate, d.lastUpdatedDate, d.lastUpdatedBy,thumbnailId, sortRank , d.privateYesNo , ou.firstname,ou.lastname, d.keyWords, d.unpublishToDo, d.internalNotes,ast.assetSelectionKey, d.internalOnly, d.recordSource union all select 'IMG' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, i.imgTitle as docTitle, i.categoryId, i.contentId, i.imgId as fileId, CAST((i.fileSize/1024) AS float) as fileSize, i.fileExt, i.imgDescription as docDescription, i.publishDate, i.unpublishDate, case when i.publishDate is not null and i.publishDate > @todayDate then 'F' when i.publishDate is not null and i.unpublishDate is not null and @todayDate >= i.publishDate and @todayDate <= i.unpublishDate then 'Y' when i.publishDate is not null and i.unpublishDate is null and i.publishDate <= @todayDate then 'Y' when i.publishDate is null then 'N' else 'N' end isPublished, i.uploadDate, i.lastUpdatedDate updateDate, i.lastUpdatedBy updateUserId, 0 restricted, -1 thumbnailId, i.highresCntId, i.sortRank, coalesce(dateDiff(day, lastUpdatedDate, @todayDate),0) as freshness, case when i.privateYesNo = 1 then 'Y' else 'N' end privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , STRING_AGG(CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END, ',') AS linked_opr , STRING_AGG(CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END, ',') AS linked_prd , STRING_AGG(CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END, ',') AS linked_sku , STRING_AGG(CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END, ',') AS linked_cdr , STRING_AGG(CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END, ',') AS linked_mfr , cast(concat('IMG',':',i.imgId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , i.internalOnly, i.recordSource , STRING_AGG(CASE WHEN c.child_attrTypeID = 23 THEN c.child_attrID ELSE NULL END, ',') AS permissionGrpIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 7 THEN c.child_attrID ELSE NULL END, ',') AS linked_brokerIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 31 THEN c.child_attrID ELSE NULL END, ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = i.imgId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CASE WHEN c.child_attrTypeID = 34 THEN c.child_attrID ELSE NULL END, ',') AS linked_UserMemberIds from tbl_IMG_Images i with (nolock) left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = i.imgId and li.child_attrTypeID in (41,59,60,63,42) and li.mstr_attrTypeID = 33 left outer JOIN tbl_ORG_AttributeLinks c ON c.mstr_attrID = i.imgId and c.child_attrTypeID in (23,7,31,34) and c.mstr_attrTypeID = 30 left outer join tbl_DOC_Categories dc with (nolock) ON c.child_attrTypeID = 31 AND dc.categoryId = c.child_attrID AND dc.ownerId = @ownerId AND dc.fsl_tablecode = @ownerType left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('IMG',':',i.imgId) where i.ownerId = @ownerId and i.fsl_tablecode = @ownerType GROUP BY i.imgTitle, i.CategoryID, i.contentId, i.imgId, i.fileSize, i.fileExt, i.imgDescription, i.publishDate, i.unpublishDate, i.uploadDate, i.lastUpdatedDate, i.lastUpdatedBy, i.highresCntId, i.sortRank, i.privateYesNo,ast.assetSelectionKey, i.internalOnly, i.recordSource ), filteredRawAssets as ( select ra.* from rawAssets ra where (1=1 or ra.linked_brokerIds in (?) ) ) , assetsWithContentURL1 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL, c.fileName as docFileName, lub.firstname + ' ' + lub.lastname as lastUpdatedByFullName from filteredRawAssets ra LEFT OUTER JOIN tbl_FSPRO_Members lub with (nolock) ON ra.updateUserId = lub.fspro_userId inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) , assetsWithContentURL2 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as thumbnailURL, c.fileName as thnFileName from assetsWithContentURL1 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.thumbnailId and c.contentId != 755 ) , assetsWithContentURL3 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as highresURL, c.fileName as highresCntFileName from assetsWithContentURL2 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.highresCntId and c.contentId != 755 ) , assets as ( select a.*, case when right( a.contentURL, charindex( '.', reverse( a.contentURL )) - 1 ) in ( 'BMP','GIF','JPEG','JPG','PNG','TIF','TIFF','WBMP' ) then left( a.contentURL, len( a.contentURL ) - charindex( '.', reverse( a.contentURL ) )) + '_50' + right( a.contentURL, charindex( '.', reverse( a.contentURL )) ) else null end previewURL from assetsWithContentURL3 a ) , categories as ( select 'OPR' contentDomain, categoryId, categoryDescription from tbl_OPR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'CDR' contentDomain, categoryId, categoryDescription from tbl_CDR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'BPL' contentDomain, categoryId, categoryDescription from tbl_BPL_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'DOC' contentDomain, categoryId, categoryName as categoryDescription from tbl_DOC_Categories with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType ) , assetsWithCategory as ( select a.*, c.categoryDescription from assets a left outer join categories c on c.contentDomain = a.contentDomain and c.categoryId = a.categoryId ) ,industryCategories as ( SELECT categoryId, categoryDescription FROM tbl_PRD_RepositoryCategories with (nolock) WHERE containerType = 'SKU' AND ownerID = 0 AND ownerType = '*' AND left(categoryDescription, 3) <> 'GS1' ) , assetsWithIndustryCategory as ( select a.*, ic.categoryDescription as industryCategoryDescription from assetsWithCategory a left outer join industryCategories ic on ic.categoryId = a.categoryId ) , assetsWithPartner as ( select a.*, opr.companyName as partnerName, a.containerId as partnerId, a.containerType as partnerType, opr.territoryId from assetsWithIndustryCategory a inner join tbl_OPR_CLientOperators opr with (nolock) on opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = a.containerId where a.containerType = 'OPR' union all select a.*, cdr.cdr_dstName as partnerName, a.containerId as partnerId, a.containerType as partnerType, cdr.cdr_territoryId as territoryId from assetsWithIndustryCategory a inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = @ownerId and cdr.fsl_tablecode = @ownerType and cdr.cdr_recordId = a.containerId where a.containerType = 'CDR' union all select a.*, mfr.mfr_name as partnerName, mfr.mfr_id as partnerId, 'MFR' as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a inner join tbl_BRO_Principals bpl with (nolock) on bpl.ownerId = @ownerId and bpl.ownerType = @ownerType and bpl.principalId = a.containerId inner join tblManufacturers mfr with (nolock) on mfr.mfr_id = bpl.mfrId where a.containerType = 'BPL' union all select a.*,cast (null as varchar ) as partnerName, cast( null as integer ) as partnerId, cast( null as varchar ) as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a where coalesce( a.containerType, '???') not in ( 'BPL', 'OPR', 'CDR' ) ), assetsWithUser as ( select a.*, m.email updateUserEmail, m.firstName + ' ' + m.lastName as updateUserFullName, case when len(isnull(a.linked_prd,'')) > 0 or len(isnull(a.linked_sku,'')) > 0 then 'Y' else 'N' end as prdLink, case when len(isnull(a.linked_opr,'')) > 0 then 'Y' else 'N' end as oprLink, case when len(isnull(a.linked_cdr,'')) > 0 then 'Y' else 'N' end as cdrLink, case when len(isnull(a.linked_mfr,'')) > 0 then 'Y' else 'N' end as mfrLink from assetsWithPartner as a left outer join tbl_Fspro_members m with (nolock) on m.ownerId > 0 and m.fspro_userId = a.updateUserId ) , assetsWithUser_mappedData as ( select * from assetsWithUser ) , assetsWithUser_preData as ( select * from assetsWithUser_mappedData with (nolock) where ( ( 1 = 1 ) and ( ( ( [isPublished] = 'Y' ) or ( [isPublished] = 'F' ) ) ) ) ) , assetsWithUser_data as ( select * from assetsWithUser_preData ) , assetsWithUser_return as ( select * from assetsWithUser_data where 1 = 1 order by docTitle offset 0 rows fetch next 50 rows only ) , assetsWithUser_result as ( select ( select count(*) from assetsWithUser_data ) as totalCount, ( select * from assetsWithUser_return for json auto, include_null_values ) as data ) select * from assetsWithUser_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER -- 2026-04-16 23:39:47.559 -- 0 {ts '2026-04-16 23:41:06'} SQL declare @ownerId int = 207 ; declare @ownerType varchar(3) = 'MFR' ; declare @contentPrefix varchar(128) = 'http://dev.fsenablers.com/FSLContent/' ; declare @todayDate datetime = ?; with docCategories as ( SELECT cats.attrID as categoryId, cats.attrDescription AS categoryName FROM tbl_ORG_Attributes cats with (nolock) WHERE cats.OwnerID = @ownerId AND cats.FSL_TableCode = @ownerType AND cats.attrTypeID = 31 ), rawAssets as ( select null contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, cast( null as varchar ) docTitle, cast( null as int) as categoryId, cast( null as int ) contentId, cast( null as int) fileId, 0.00 fileSize, null fileExt, null docDescription, cast( null as datetime ) publishDate, cast( null as datetime ) unpublishDate, 'N' isPublished, cast( null as datetime ) uploadDate, cast( null as datetime ) updateDate, cast( null as int ) updateUserId, 0 restricted, -1 thumbnailId, -1 highresCntId, 99 as sortRank , 0 as freshness , 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr, cast( null as varchar ) as assetSelectionKey, 0 as selectedRecord , 'N' as internalOnly, '' as recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds union all select 'DOC' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, d.docTitle, d.repCategoryId as categoryId, d.contentId, d.docId as fileId, CAST((d.fileSize/1024) AS float) as fileSize , d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, case when d.publishDate is not null and d.publishDate > @todayDate then 'F' when d.publishDate is not null and d.unpublishDate is not null and @todayDate >= d.publishDate and @todayDate <= d.unpublishDate then 'Y' when d.publishDate is not null and d.unpublishDate is null and d.publishDate <= @todayDate then 'Y' when d.publishDate is null then 'N' else 'N' end isPublished, d.uploadDate, d.lastUpdatedDate updateDate, d.lastUpdatedBy updateUserId, 0 restricted, thumbnailId, -1 highresCntId, sortRank , coalesce(dateDiff(day, d.lastUpdatedDate, @todayDate),0) as freshness, case when d.privateYesNo = 1 then 'Y' else 'N' end privateYesNo, ou.firstname + ' ' + ou.lastname as uploadedByFullName, d.keyWords, d.unpublishToDo, d.internalNotes , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END), ',') AS linked_opr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END), ',') AS linked_prd , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END), ',') AS linked_sku , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END), ',') AS linked_cdr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END), ',') AS linked_mfr , cast(concat('DOC',':',d.docId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , d.internalOnly, d.recordSource , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 23 THEN li.child_attrID ELSE NULL END), ',') AS permissionGrpIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 7 THEN li.child_attrID ELSE NULL END), ',') AS linked_brokerIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 31 THEN li.child_attrID ELSE NULL END), ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = d.docId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 34 THEN li.child_attrID ELSE NULL END), ',') AS linked_UserMemberIds from tbl_DOC_Documents d with (nolock) LEFT OUTER JOIN tbl_FSPRO_Members ou with (nolock) ON d.AuthorUserId = ou.fspro_userId left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = d.docId and li.child_attrTypeID in (41,59,60,63,42,23,7,31,34) and li.mstr_attrTypeID = 30 left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('DOC',':',d.docId) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType GROUP BY d.docTitle, d.repCategoryId, d.contentId, d.docId,d.fileSize, d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, d.uploadDate, d.lastUpdatedDate, d.lastUpdatedBy,thumbnailId, sortRank , d.privateYesNo , ou.firstname,ou.lastname, d.keyWords, d.unpublishToDo, d.internalNotes,ast.assetSelectionKey, d.internalOnly, d.recordSource union all select 'IMG' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, i.imgTitle as docTitle, i.categoryId, i.contentId, i.imgId as fileId, CAST((i.fileSize/1024) AS float) as fileSize, i.fileExt, i.imgDescription as docDescription, i.publishDate, i.unpublishDate, case when i.publishDate is not null and i.publishDate > @todayDate then 'F' when i.publishDate is not null and i.unpublishDate is not null and @todayDate >= i.publishDate and @todayDate <= i.unpublishDate then 'Y' when i.publishDate is not null and i.unpublishDate is null and i.publishDate <= @todayDate then 'Y' when i.publishDate is null then 'N' else 'N' end isPublished, i.uploadDate, i.lastUpdatedDate updateDate, i.lastUpdatedBy updateUserId, 0 restricted, -1 thumbnailId, i.highresCntId, i.sortRank, coalesce(dateDiff(day, lastUpdatedDate, @todayDate),0) as freshness, case when i.privateYesNo = 1 then 'Y' else 'N' end privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , STRING_AGG(CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END, ',') AS linked_opr , STRING_AGG(CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END, ',') AS linked_prd , STRING_AGG(CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END, ',') AS linked_sku , STRING_AGG(CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END, ',') AS linked_cdr , STRING_AGG(CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END, ',') AS linked_mfr , cast(concat('IMG',':',i.imgId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , i.internalOnly, i.recordSource , STRING_AGG(CASE WHEN c.child_attrTypeID = 23 THEN c.child_attrID ELSE NULL END, ',') AS permissionGrpIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 7 THEN c.child_attrID ELSE NULL END, ',') AS linked_brokerIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 31 THEN c.child_attrID ELSE NULL END, ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = i.imgId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CASE WHEN c.child_attrTypeID = 34 THEN c.child_attrID ELSE NULL END, ',') AS linked_UserMemberIds from tbl_IMG_Images i with (nolock) left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = i.imgId and li.child_attrTypeID in (41,59,60,63,42) and li.mstr_attrTypeID = 33 left outer JOIN tbl_ORG_AttributeLinks c ON c.mstr_attrID = i.imgId and c.child_attrTypeID in (23,7,31,34) and c.mstr_attrTypeID = 30 left outer join tbl_DOC_Categories dc with (nolock) ON c.child_attrTypeID = 31 AND dc.categoryId = c.child_attrID AND dc.ownerId = @ownerId AND dc.fsl_tablecode = @ownerType left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('IMG',':',i.imgId) where i.ownerId = @ownerId and i.fsl_tablecode = @ownerType GROUP BY i.imgTitle, i.CategoryID, i.contentId, i.imgId, i.fileSize, i.fileExt, i.imgDescription, i.publishDate, i.unpublishDate, i.uploadDate, i.lastUpdatedDate, i.lastUpdatedBy, i.highresCntId, i.sortRank, i.privateYesNo,ast.assetSelectionKey, i.internalOnly, i.recordSource ), filteredRawAssets as ( select ra.* from rawAssets ra where (1=1 or ra.linked_brokerIds in (?) ) ) , assetsWithContentURL1 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL, c.fileName as docFileName, lub.firstname + ' ' + lub.lastname as lastUpdatedByFullName from filteredRawAssets ra LEFT OUTER JOIN tbl_FSPRO_Members lub with (nolock) ON ra.updateUserId = lub.fspro_userId inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) , assetsWithContentURL2 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as thumbnailURL, c.fileName as thnFileName from assetsWithContentURL1 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.thumbnailId and c.contentId != 755 ) , assetsWithContentURL3 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as highresURL, c.fileName as highresCntFileName from assetsWithContentURL2 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.highresCntId and c.contentId != 755 ) , assets as ( select a.*, case when right( a.contentURL, charindex( '.', reverse( a.contentURL )) - 1 ) in ( 'BMP','GIF','JPEG','JPG','PNG','TIF','TIFF','WBMP' ) then left( a.contentURL, len( a.contentURL ) - charindex( '.', reverse( a.contentURL ) )) + '_50' + right( a.contentURL, charindex( '.', reverse( a.contentURL )) ) else null end previewURL from assetsWithContentURL3 a ) , categories as ( select 'OPR' contentDomain, categoryId, categoryDescription from tbl_OPR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'CDR' contentDomain, categoryId, categoryDescription from tbl_CDR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'BPL' contentDomain, categoryId, categoryDescription from tbl_BPL_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'DOC' contentDomain, categoryId, categoryName as categoryDescription from tbl_DOC_Categories with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType ) , assetsWithCategory as ( select a.*, c.categoryDescription from assets a left outer join categories c on c.contentDomain = a.contentDomain and c.categoryId = a.categoryId ) ,industryCategories as ( SELECT categoryId, categoryDescription FROM tbl_PRD_RepositoryCategories with (nolock) WHERE containerType = 'SKU' AND ownerID = 0 AND ownerType = '*' AND left(categoryDescription, 3) <> 'GS1' ) , assetsWithIndustryCategory as ( select a.*, ic.categoryDescription as industryCategoryDescription from assetsWithCategory a left outer join industryCategories ic on ic.categoryId = a.categoryId ) , assetsWithPartner as ( select a.*, opr.companyName as partnerName, a.containerId as partnerId, a.containerType as partnerType, opr.territoryId from assetsWithIndustryCategory a inner join tbl_OPR_CLientOperators opr with (nolock) on opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = a.containerId where a.containerType = 'OPR' union all select a.*, cdr.cdr_dstName as partnerName, a.containerId as partnerId, a.containerType as partnerType, cdr.cdr_territoryId as territoryId from assetsWithIndustryCategory a inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = @ownerId and cdr.fsl_tablecode = @ownerType and cdr.cdr_recordId = a.containerId where a.containerType = 'CDR' union all select a.*, mfr.mfr_name as partnerName, mfr.mfr_id as partnerId, 'MFR' as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a inner join tbl_BRO_Principals bpl with (nolock) on bpl.ownerId = @ownerId and bpl.ownerType = @ownerType and bpl.principalId = a.containerId inner join tblManufacturers mfr with (nolock) on mfr.mfr_id = bpl.mfrId where a.containerType = 'BPL' union all select a.*,cast (null as varchar ) as partnerName, cast( null as integer ) as partnerId, cast( null as varchar ) as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a where coalesce( a.containerType, '???') not in ( 'BPL', 'OPR', 'CDR' ) ), assetsWithUser as ( select a.*, m.email updateUserEmail, m.firstName + ' ' + m.lastName as updateUserFullName, case when len(isnull(a.linked_prd,'')) > 0 or len(isnull(a.linked_sku,'')) > 0 then 'Y' else 'N' end as prdLink, case when len(isnull(a.linked_opr,'')) > 0 then 'Y' else 'N' end as oprLink, case when len(isnull(a.linked_cdr,'')) > 0 then 'Y' else 'N' end as cdrLink, case when len(isnull(a.linked_mfr,'')) > 0 then 'Y' else 'N' end as mfrLink from assetsWithPartner as a left outer join tbl_Fspro_members m with (nolock) on m.ownerId > 0 and m.fspro_userId = a.updateUserId ) , assetsWithUser_mappedData as ( select * from assetsWithUser ) , assetsWithUser_preData as ( select * from assetsWithUser_mappedData with (nolock) where ( ( 1 = 1 ) and ( ( ( [isPublished] = 'Y' ) or ( [isPublished] = 'F' ) ) ) ) ) , assetsWithUser_data as ( select * from assetsWithUser_preData ) , assetsWithUser_return as ( select * from assetsWithUser_data where 1 = 1 order by docTitle offset 0 rows fetch next 50 rows only ) , assetsWithUser_result as ( select ( select count(*) from assetsWithUser_data ) as totalCount, ( select * from assetsWithUser_return for json auto, include_null_values ) as data ) select * from assetsWithUser_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER -- 2026-04-16 23:41:06.692 -- 0 {ts '2026-04-16 23:41:42'} SQL declare @ownerId int = 207 ; declare @ownerType varchar(3) = 'MFR' ; declare @contentPrefix varchar(128) = 'http://dev.fsenablers.com/FSLContent/' ; declare @todayDate datetime = ?; with docCategories as ( SELECT cats.attrID as categoryId, cats.attrDescription AS categoryName FROM tbl_ORG_Attributes cats with (nolock) WHERE cats.OwnerID = @ownerId AND cats.FSL_TableCode = @ownerType AND cats.attrTypeID = 31 ), rawAssets as ( select null contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, cast( null as varchar ) docTitle, cast( null as int) as categoryId, cast( null as int ) contentId, cast( null as int) fileId, 0.00 fileSize, null fileExt, null docDescription, cast( null as datetime ) publishDate, cast( null as datetime ) unpublishDate, 'N' isPublished, cast( null as datetime ) uploadDate, cast( null as datetime ) updateDate, cast( null as int ) updateUserId, 0 restricted, -1 thumbnailId, -1 highresCntId, 99 as sortRank , 0 as freshness , 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr, cast( null as varchar ) as assetSelectionKey, 0 as selectedRecord , 'N' as internalOnly, '' as recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds union all select 'DOC' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, d.docTitle, d.repCategoryId as categoryId, d.contentId, d.docId as fileId, CAST((d.fileSize/1024) AS float) as fileSize , d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, case when d.publishDate is not null and d.publishDate > @todayDate then 'F' when d.publishDate is not null and d.unpublishDate is not null and @todayDate >= d.publishDate and @todayDate <= d.unpublishDate then 'Y' when d.publishDate is not null and d.unpublishDate is null and d.publishDate <= @todayDate then 'Y' when d.publishDate is null then 'N' else 'N' end isPublished, d.uploadDate, d.lastUpdatedDate updateDate, d.lastUpdatedBy updateUserId, 0 restricted, thumbnailId, -1 highresCntId, sortRank , coalesce(dateDiff(day, d.lastUpdatedDate, @todayDate),0) as freshness, case when d.privateYesNo = 1 then 'Y' else 'N' end privateYesNo, ou.firstname + ' ' + ou.lastname as uploadedByFullName, d.keyWords, d.unpublishToDo, d.internalNotes , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END), ',') AS linked_opr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END), ',') AS linked_prd , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END), ',') AS linked_sku , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END), ',') AS linked_cdr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END), ',') AS linked_mfr , cast(concat('DOC',':',d.docId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , d.internalOnly, d.recordSource , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 23 THEN li.child_attrID ELSE NULL END), ',') AS permissionGrpIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 7 THEN li.child_attrID ELSE NULL END), ',') AS linked_brokerIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 31 THEN li.child_attrID ELSE NULL END), ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = d.docId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 34 THEN li.child_attrID ELSE NULL END), ',') AS linked_UserMemberIds from tbl_DOC_Documents d with (nolock) LEFT OUTER JOIN tbl_FSPRO_Members ou with (nolock) ON d.AuthorUserId = ou.fspro_userId left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = d.docId and li.child_attrTypeID in (41,59,60,63,42,23,7,31,34) and li.mstr_attrTypeID = 30 left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('DOC',':',d.docId) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType GROUP BY d.docTitle, d.repCategoryId, d.contentId, d.docId,d.fileSize, d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, d.uploadDate, d.lastUpdatedDate, d.lastUpdatedBy,thumbnailId, sortRank , d.privateYesNo , ou.firstname,ou.lastname, d.keyWords, d.unpublishToDo, d.internalNotes,ast.assetSelectionKey, d.internalOnly, d.recordSource union all select 'IMG' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, i.imgTitle as docTitle, i.categoryId, i.contentId, i.imgId as fileId, CAST((i.fileSize/1024) AS float) as fileSize, i.fileExt, i.imgDescription as docDescription, i.publishDate, i.unpublishDate, case when i.publishDate is not null and i.publishDate > @todayDate then 'F' when i.publishDate is not null and i.unpublishDate is not null and @todayDate >= i.publishDate and @todayDate <= i.unpublishDate then 'Y' when i.publishDate is not null and i.unpublishDate is null and i.publishDate <= @todayDate then 'Y' when i.publishDate is null then 'N' else 'N' end isPublished, i.uploadDate, i.lastUpdatedDate updateDate, i.lastUpdatedBy updateUserId, 0 restricted, -1 thumbnailId, i.highresCntId, i.sortRank, coalesce(dateDiff(day, lastUpdatedDate, @todayDate),0) as freshness, case when i.privateYesNo = 1 then 'Y' else 'N' end privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , STRING_AGG(CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END, ',') AS linked_opr , STRING_AGG(CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END, ',') AS linked_prd , STRING_AGG(CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END, ',') AS linked_sku , STRING_AGG(CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END, ',') AS linked_cdr , STRING_AGG(CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END, ',') AS linked_mfr , cast(concat('IMG',':',i.imgId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , i.internalOnly, i.recordSource , STRING_AGG(CASE WHEN c.child_attrTypeID = 23 THEN c.child_attrID ELSE NULL END, ',') AS permissionGrpIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 7 THEN c.child_attrID ELSE NULL END, ',') AS linked_brokerIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 31 THEN c.child_attrID ELSE NULL END, ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = i.imgId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CASE WHEN c.child_attrTypeID = 34 THEN c.child_attrID ELSE NULL END, ',') AS linked_UserMemberIds from tbl_IMG_Images i with (nolock) left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = i.imgId and li.child_attrTypeID in (41,59,60,63,42) and li.mstr_attrTypeID = 33 left outer JOIN tbl_ORG_AttributeLinks c ON c.mstr_attrID = i.imgId and c.child_attrTypeID in (23,7,31,34) and c.mstr_attrTypeID = 30 left outer join tbl_DOC_Categories dc with (nolock) ON c.child_attrTypeID = 31 AND dc.categoryId = c.child_attrID AND dc.ownerId = @ownerId AND dc.fsl_tablecode = @ownerType left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('IMG',':',i.imgId) where i.ownerId = @ownerId and i.fsl_tablecode = @ownerType GROUP BY i.imgTitle, i.CategoryID, i.contentId, i.imgId, i.fileSize, i.fileExt, i.imgDescription, i.publishDate, i.unpublishDate, i.uploadDate, i.lastUpdatedDate, i.lastUpdatedBy, i.highresCntId, i.sortRank, i.privateYesNo,ast.assetSelectionKey, i.internalOnly, i.recordSource ), filteredRawAssets as ( select ra.* from rawAssets ra where (1=1 or ra.linked_brokerIds in (?) ) ) , assetsWithContentURL1 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL, c.fileName as docFileName, lub.firstname + ' ' + lub.lastname as lastUpdatedByFullName from filteredRawAssets ra LEFT OUTER JOIN tbl_FSPRO_Members lub with (nolock) ON ra.updateUserId = lub.fspro_userId inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) , assetsWithContentURL2 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as thumbnailURL, c.fileName as thnFileName from assetsWithContentURL1 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.thumbnailId and c.contentId != 755 ) , assetsWithContentURL3 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as highresURL, c.fileName as highresCntFileName from assetsWithContentURL2 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.highresCntId and c.contentId != 755 ) , assets as ( select a.*, case when right( a.contentURL, charindex( '.', reverse( a.contentURL )) - 1 ) in ( 'BMP','GIF','JPEG','JPG','PNG','TIF','TIFF','WBMP' ) then left( a.contentURL, len( a.contentURL ) - charindex( '.', reverse( a.contentURL ) )) + '_50' + right( a.contentURL, charindex( '.', reverse( a.contentURL )) ) else null end previewURL from assetsWithContentURL3 a ) , categories as ( select 'OPR' contentDomain, categoryId, categoryDescription from tbl_OPR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'CDR' contentDomain, categoryId, categoryDescription from tbl_CDR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'BPL' contentDomain, categoryId, categoryDescription from tbl_BPL_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'DOC' contentDomain, categoryId, categoryName as categoryDescription from tbl_DOC_Categories with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType ) , assetsWithCategory as ( select a.*, c.categoryDescription from assets a left outer join categories c on c.contentDomain = a.contentDomain and c.categoryId = a.categoryId ) ,industryCategories as ( SELECT categoryId, categoryDescription FROM tbl_PRD_RepositoryCategories with (nolock) WHERE containerType = 'SKU' AND ownerID = 0 AND ownerType = '*' AND left(categoryDescription, 3) <> 'GS1' ) , assetsWithIndustryCategory as ( select a.*, ic.categoryDescription as industryCategoryDescription from assetsWithCategory a left outer join industryCategories ic on ic.categoryId = a.categoryId ) , assetsWithPartner as ( select a.*, opr.companyName as partnerName, a.containerId as partnerId, a.containerType as partnerType, opr.territoryId from assetsWithIndustryCategory a inner join tbl_OPR_CLientOperators opr with (nolock) on opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = a.containerId where a.containerType = 'OPR' union all select a.*, cdr.cdr_dstName as partnerName, a.containerId as partnerId, a.containerType as partnerType, cdr.cdr_territoryId as territoryId from assetsWithIndustryCategory a inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = @ownerId and cdr.fsl_tablecode = @ownerType and cdr.cdr_recordId = a.containerId where a.containerType = 'CDR' union all select a.*, mfr.mfr_name as partnerName, mfr.mfr_id as partnerId, 'MFR' as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a inner join tbl_BRO_Principals bpl with (nolock) on bpl.ownerId = @ownerId and bpl.ownerType = @ownerType and bpl.principalId = a.containerId inner join tblManufacturers mfr with (nolock) on mfr.mfr_id = bpl.mfrId where a.containerType = 'BPL' union all select a.*,cast (null as varchar ) as partnerName, cast( null as integer ) as partnerId, cast( null as varchar ) as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a where coalesce( a.containerType, '???') not in ( 'BPL', 'OPR', 'CDR' ) ), assetsWithUser as ( select a.*, m.email updateUserEmail, m.firstName + ' ' + m.lastName as updateUserFullName, case when len(isnull(a.linked_prd,'')) > 0 or len(isnull(a.linked_sku,'')) > 0 then 'Y' else 'N' end as prdLink, case when len(isnull(a.linked_opr,'')) > 0 then 'Y' else 'N' end as oprLink, case when len(isnull(a.linked_cdr,'')) > 0 then 'Y' else 'N' end as cdrLink, case when len(isnull(a.linked_mfr,'')) > 0 then 'Y' else 'N' end as mfrLink from assetsWithPartner as a left outer join tbl_Fspro_members m with (nolock) on m.ownerId > 0 and m.fspro_userId = a.updateUserId ) , assetsWithUser_mappedData as ( select * from assetsWithUser ) , assetsWithUser_preData as ( select * from assetsWithUser_mappedData with (nolock) where ( ( 1 = 1 ) and ( ( ( [isPublished] = 'Y' ) or ( [isPublished] = 'F' ) ) ) ) ) , assetsWithUser_data as ( select * from assetsWithUser_preData ) , assetsWithUser_return as ( select * from assetsWithUser_data where 1 = 1 order by docTitle offset 0 rows fetch next 50 rows only ) , assetsWithUser_result as ( select ( select count(*) from assetsWithUser_data ) as totalCount, ( select * from assetsWithUser_return for json auto, include_null_values ) as data ) select * from assetsWithUser_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER -- 2026-04-16 23:41:42.315 -- 0 {ts '2026-04-16 23:42:38'} SQL declare @ownerId int = 207 ; declare @ownerType varchar(3) = 'MFR' ; declare @contentPrefix varchar(128) = 'http://dev.fsenablers.com/FSLContent/' ; declare @todayDate datetime = ?; with docCategories as ( SELECT cats.attrID as categoryId, cats.attrDescription AS categoryName FROM tbl_ORG_Attributes cats with (nolock) WHERE cats.OwnerID = @ownerId AND cats.FSL_TableCode = @ownerType AND cats.attrTypeID = 31 ), rawAssets as ( select null contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, cast( null as varchar ) docTitle, cast( null as int) as categoryId, cast( null as int ) contentId, cast( null as int) fileId, 0.00 fileSize, null fileExt, null docDescription, cast( null as datetime ) publishDate, cast( null as datetime ) unpublishDate, 'N' isPublished, cast( null as datetime ) uploadDate, cast( null as datetime ) updateDate, cast( null as int ) updateUserId, 0 restricted, -1 thumbnailId, -1 highresCntId, 99 as sortRank , 0 as freshness , 'N' privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , cast( null as varchar ) AS linked_opr, cast( null as varchar ) AS linked_prd, cast( null as varchar ) AS linked_sku , cast( null as varchar ) AS linked_cdr, cast( null as varchar ) AS linked_mfr, cast( null as varchar ) as assetSelectionKey, 0 as selectedRecord , 'N' as internalOnly, '' as recordSource , cast( null as varchar ) permissionGrpIds , cast( null as varchar ) linked_brokerIds, cast( null as varchar ) linked_catIds, cast( null as varchar ) linked_catNames, cast( null as varchar ) linked_UserMemberIds union all select 'DOC' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, d.docTitle, d.repCategoryId as categoryId, d.contentId, d.docId as fileId, CAST((d.fileSize/1024) AS float) as fileSize , d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, case when d.publishDate is not null and d.publishDate > @todayDate then 'F' when d.publishDate is not null and d.unpublishDate is not null and @todayDate >= d.publishDate and @todayDate <= d.unpublishDate then 'Y' when d.publishDate is not null and d.unpublishDate is null and d.publishDate <= @todayDate then 'Y' when d.publishDate is null then 'N' else 'N' end isPublished, d.uploadDate, d.lastUpdatedDate updateDate, d.lastUpdatedBy updateUserId, 0 restricted, thumbnailId, -1 highresCntId, sortRank , coalesce(dateDiff(day, d.lastUpdatedDate, @todayDate),0) as freshness, case when d.privateYesNo = 1 then 'Y' else 'N' end privateYesNo, ou.firstname + ' ' + ou.lastname as uploadedByFullName, d.keyWords, d.unpublishToDo, d.internalNotes , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END), ',') AS linked_opr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END), ',') AS linked_prd , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END), ',') AS linked_sku , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END), ',') AS linked_cdr , STRING_AGG(CONVERT(NVARCHAR(MAX),CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END), ',') AS linked_mfr , cast(concat('DOC',':',d.docId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , d.internalOnly, d.recordSource , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 23 THEN li.child_attrID ELSE NULL END), ',') AS permissionGrpIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 7 THEN li.child_attrID ELSE NULL END), ',') AS linked_brokerIds , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 31 THEN li.child_attrID ELSE NULL END), ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = d.docId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CONVERT(NVARCHAR(MAX), CASE WHEN li.child_attrTypeID = 34 THEN li.child_attrID ELSE NULL END), ',') AS linked_UserMemberIds from tbl_DOC_Documents d with (nolock) LEFT OUTER JOIN tbl_FSPRO_Members ou with (nolock) ON d.AuthorUserId = ou.fspro_userId left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = d.docId and li.child_attrTypeID in (41,59,60,63,42,23,7,31,34) and li.mstr_attrTypeID = 30 left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('DOC',':',d.docId) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType GROUP BY d.docTitle, d.repCategoryId, d.contentId, d.docId,d.fileSize, d.fileExt, d.docDescription, d.publishDate, d.unpublishDate, d.uploadDate, d.lastUpdatedDate, d.lastUpdatedBy,thumbnailId, sortRank , d.privateYesNo , ou.firstname,ou.lastname, d.keyWords, d.unpublishToDo, d.internalNotes,ast.assetSelectionKey, d.internalOnly, d.recordSource union all select 'IMG' contentDomain, cast( null as int ) containerId, cast( null as varchar ) containerType, i.imgTitle as docTitle, i.categoryId, i.contentId, i.imgId as fileId, CAST((i.fileSize/1024) AS float) as fileSize, i.fileExt, i.imgDescription as docDescription, i.publishDate, i.unpublishDate, case when i.publishDate is not null and i.publishDate > @todayDate then 'F' when i.publishDate is not null and i.unpublishDate is not null and @todayDate >= i.publishDate and @todayDate <= i.unpublishDate then 'Y' when i.publishDate is not null and i.unpublishDate is null and i.publishDate <= @todayDate then 'Y' when i.publishDate is null then 'N' else 'N' end isPublished, i.uploadDate, i.lastUpdatedDate updateDate, i.lastUpdatedBy updateUserId, 0 restricted, -1 thumbnailId, i.highresCntId, i.sortRank, coalesce(dateDiff(day, lastUpdatedDate, @todayDate),0) as freshness, case when i.privateYesNo = 1 then 'Y' else 'N' end privateYesNo , cast( null as varchar ) uploadedByFullName, cast( null as varchar ) keyWords, cast('' as varchar ) unpublishToDo, cast( null as varchar ) internalNotes , STRING_AGG(CASE WHEN li.child_attrTypeID = 41 THEN li.child_attrID ELSE NULL END, ',') AS linked_opr , STRING_AGG(CASE WHEN li.child_attrTypeID = 59 THEN li.child_attrID ELSE NULL END, ',') AS linked_prd , STRING_AGG(CASE WHEN li.child_attrTypeID = 60 THEN li.child_attrID ELSE NULL END, ',') AS linked_sku , STRING_AGG(CASE WHEN li.child_attrTypeID = 63 THEN li.child_attrID ELSE NULL END, ',') AS linked_cdr , STRING_AGG(CASE WHEN li.child_attrTypeID = 42 THEN li.child_attrID ELSE NULL END, ',') AS linked_mfr , cast(concat('IMG',':',i.imgId) as varchar) as assetSelectionKey , cast( case when ast.assetSelectionKey is not null then 1 else 0 end as bit ) as selectedRecord , i.internalOnly, i.recordSource , STRING_AGG(CASE WHEN c.child_attrTypeID = 23 THEN c.child_attrID ELSE NULL END, ',') AS permissionGrpIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 7 THEN c.child_attrID ELSE NULL END, ',') AS linked_brokerIds , STRING_AGG(CASE WHEN c.child_attrTypeID = 31 THEN c.child_attrID ELSE NULL END, ',') AS linked_catIds , ( SELECT STRING_AGG(cat.categoryName, ',') FROM tbl_ORG_AttributeLinks catLi INNER JOIN docCategories cat ON cat.categoryId = catLi.child_attrID WHERE catLi.mstr_attrID = i.imgId AND catLi.child_attrTypeID = 31 AND catLi.mstr_attrTypeID = 30 ) AS linked_catNames , STRING_AGG(CASE WHEN c.child_attrTypeID = 34 THEN c.child_attrID ELSE NULL END, ',') AS linked_UserMemberIds from tbl_IMG_Images i with (nolock) left outer JOIN tbl_ORG_AttributeLinks li ON li.mstr_attrID = i.imgId and li.child_attrTypeID in (41,59,60,63,42) and li.mstr_attrTypeID = 33 left outer JOIN tbl_ORG_AttributeLinks c ON c.mstr_attrID = i.imgId and c.child_attrTypeID in (23,7,31,34) and c.mstr_attrTypeID = 30 left outer join tbl_DOC_Categories dc with (nolock) ON c.child_attrTypeID = 31 AND dc.categoryId = c.child_attrID AND dc.ownerId = @ownerId AND dc.fsl_tablecode = @ownerType left outer join tmp_assetsSelections_MFR207__14042379 ast with (nolock) on ast.assetSelectionKey = concat('IMG',':',i.imgId) where i.ownerId = @ownerId and i.fsl_tablecode = @ownerType GROUP BY i.imgTitle, i.CategoryID, i.contentId, i.imgId, i.fileSize, i.fileExt, i.imgDescription, i.publishDate, i.unpublishDate, i.uploadDate, i.lastUpdatedDate, i.lastUpdatedBy, i.highresCntId, i.sortRank, i.privateYesNo,ast.assetSelectionKey, i.internalOnly, i.recordSource ), filteredRawAssets as ( select ra.* from rawAssets ra where (1=1 or ra.linked_brokerIds in (?) ) ) , assetsWithContentURL1 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL, c.fileName as docFileName, lub.firstname + ' ' + lub.lastname as lastUpdatedByFullName from filteredRawAssets ra LEFT OUTER JOIN tbl_FSPRO_Members lub with (nolock) ON ra.updateUserId = lub.fspro_userId inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) , assetsWithContentURL2 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as thumbnailURL, c.fileName as thnFileName from assetsWithContentURL1 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.thumbnailId and c.contentId != 755 ) , assetsWithContentURL3 as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as highresURL, c.fileName as highresCntFileName from assetsWithContentURL2 ra left outer join tbl_FSC_Content c with (nolock) on c.contentId = ra.highresCntId and c.contentId != 755 ) , assets as ( select a.*, case when right( a.contentURL, charindex( '.', reverse( a.contentURL )) - 1 ) in ( 'BMP','GIF','JPEG','JPG','PNG','TIF','TIFF','WBMP' ) then left( a.contentURL, len( a.contentURL ) - charindex( '.', reverse( a.contentURL ) )) + '_50' + right( a.contentURL, charindex( '.', reverse( a.contentURL )) ) else null end previewURL from assetsWithContentURL3 a ) , categories as ( select 'OPR' contentDomain, categoryId, categoryDescription from tbl_OPR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'CDR' contentDomain, categoryId, categoryDescription from tbl_CDR_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'BPL' contentDomain, categoryId, categoryDescription from tbl_BPL_RepositoryCategories with (nolock) where ownerId = @ownerId and ownerType = @ownerType union all select 'DOC' contentDomain, categoryId, categoryName as categoryDescription from tbl_DOC_Categories with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType ) , assetsWithCategory as ( select a.*, c.categoryDescription from assets a left outer join categories c on c.contentDomain = a.contentDomain and c.categoryId = a.categoryId ) ,industryCategories as ( SELECT categoryId, categoryDescription FROM tbl_PRD_RepositoryCategories with (nolock) WHERE containerType = 'SKU' AND ownerID = 0 AND ownerType = '*' AND left(categoryDescription, 3) <> 'GS1' ) , assetsWithIndustryCategory as ( select a.*, ic.categoryDescription as industryCategoryDescription from assetsWithCategory a left outer join industryCategories ic on ic.categoryId = a.categoryId ) , assetsWithPartner as ( select a.*, opr.companyName as partnerName, a.containerId as partnerId, a.containerType as partnerType, opr.territoryId from assetsWithIndustryCategory a inner join tbl_OPR_CLientOperators opr with (nolock) on opr.ownerId = @ownerId and opr.fsltablecode = @ownerType and opr.operatorId = a.containerId where a.containerType = 'OPR' union all select a.*, cdr.cdr_dstName as partnerName, a.containerId as partnerId, a.containerType as partnerType, cdr.cdr_territoryId as territoryId from assetsWithIndustryCategory a inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = @ownerId and cdr.fsl_tablecode = @ownerType and cdr.cdr_recordId = a.containerId where a.containerType = 'CDR' union all select a.*, mfr.mfr_name as partnerName, mfr.mfr_id as partnerId, 'MFR' as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a inner join tbl_BRO_Principals bpl with (nolock) on bpl.ownerId = @ownerId and bpl.ownerType = @ownerType and bpl.principalId = a.containerId inner join tblManufacturers mfr with (nolock) on mfr.mfr_id = bpl.mfrId where a.containerType = 'BPL' union all select a.*,cast (null as varchar ) as partnerName, cast( null as integer ) as partnerId, cast( null as varchar ) as partnerType, cast( null as integer ) territoryId from assetsWithIndustryCategory a where coalesce( a.containerType, '???') not in ( 'BPL', 'OPR', 'CDR' ) ), assetsWithUser as ( select a.*, m.email updateUserEmail, m.firstName + ' ' + m.lastName as updateUserFullName, case when len(isnull(a.linked_prd,'')) > 0 or len(isnull(a.linked_sku,'')) > 0 then 'Y' else 'N' end as prdLink, case when len(isnull(a.linked_opr,'')) > 0 then 'Y' else 'N' end as oprLink, case when len(isnull(a.linked_cdr,'')) > 0 then 'Y' else 'N' end as cdrLink, case when len(isnull(a.linked_mfr,'')) > 0 then 'Y' else 'N' end as mfrLink from assetsWithPartner as a left outer join tbl_Fspro_members m with (nolock) on m.ownerId > 0 and m.fspro_userId = a.updateUserId ) , assetsWithUser_mappedData as ( select * from assetsWithUser ) , assetsWithUser_preData as ( select * from assetsWithUser_mappedData with (nolock) where ( ( 1 = 1 ) and ( ( ( [isPublished] = 'Y' ) or ( [isPublished] = 'F' ) ) ) ) ) , assetsWithUser_data as ( select * from assetsWithUser_preData ) , assetsWithUser_return as ( select * from assetsWithUser_data where 1 = 1 order by docTitle offset 0 rows fetch next 50 rows only ) , assetsWithUser_result as ( select ( select count(*) from assetsWithUser_data ) as totalCount, ( select * from assetsWithUser_return for json auto, include_null_values ) as data ) select * from assetsWithUser_result for JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER -- 2026-04-16 23:42:37.963 -- 0