Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create model for supporting document sets (Add and update) #866

Merged
merged 5 commits into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
205 changes: 205 additions & 0 deletions src/sdk/PnP.Core.Test/SharePoint/ContentTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;

namespace PnP.Core.Test.SharePoint
{
[TestClass]
public class ContentTypeTests
{
private const string docSetId = "0x0120D520002E5AAE78D8DB1947903FC99D0979E4A5";

[ClassInitialize]
public static void TestFixtureSetup(TestContext context)
{
Expand Down Expand Up @@ -790,5 +793,207 @@ public async Task ContentTypesOrderingOnListTest()
}
}

#region Document Sets

[TestMethod]
public async Task GetContentTypeAsDocumentSet()
{
//TestCommon.Instance.Mocking = false;
using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite))
{
IContentType contentType = (from ct in context.Web.ContentTypes
where ct.StringId == "0x0120D520"
select ct)
.QueryProperties(ct => ct.StringId, ct => ct.Id)
.FirstOrDefault();

Assert.IsNotNull(contentType);
// Test Id property
Assert.AreEqual(contentType.Id, "0x0120D520");

var documentSet = contentType.AsDocumentSet();

Assert.AreEqual(documentSet.ContentTypeId, contentType.Id);
Assert.IsNotNull(documentSet.DefaultContents);
Assert.IsNotNull(documentSet.SharedColumns);
Assert.IsNotNull(documentSet.WelcomePageColumns);
Assert.IsNotNull(documentSet.AllowedContentTypes);
}
}

[TestMethod]
public async Task AddContentTypeAsDocumentSet()
{
//TestCommon.Instance.Mocking = false;
using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite))
{
var list = await context.Web.Lists.GetByTitle("Documents").GetAsync();
var rootFolder = await list.RootFolder.GetAsync();

(_, _, string documentUrl) = await TestAssets.CreateTestDocumentAsync(1, parentFolder: rootFolder);

var categoriesField = await context.Web.Fields.FirstAsync(y => y.InternalName == "Categories").ConfigureAwait(false);
var managersField = await context.Web.Fields.FirstAsync(y => y.InternalName == "ManagersName").ConfigureAwait(false);
var file = await context.Web.GetFileByServerRelativeUrlAsync(documentUrl).ConfigureAwait(false);
var documentCt = await context.Web.ContentTypes.FirstAsync(y => y.Name == "Document").ConfigureAwait(false);
var formCt = await context.Web.ContentTypes.FirstAsync(y => y.Name == "Form").ConfigureAwait(false);

var documentSetOptions = new DocumentSetOptions
{
AllowedContentTypes = new List<IContentType>
{
documentCt,
formCt
},
ShouldPrefixNameToFile = true,
PropagateWelcomePageChanges = true,
SharedColumns = new List<IField>
{
managersField,
categoriesField
},
WelcomePageColumns = new List<IField>
{
managersField,
categoriesField
},
DefaultContents = new List<DocumentSetContentOptions>
{
new DocumentSetContentOptions
{
FileName = "Test.docx",
FolderName = "FolderName",
File = file,
ContentTypeId = documentCt.StringId
}
}
};

IDocumentSet newDocumentSet = await context.Web.ContentTypes.AddDocumentSetAsync(docSetId, "Document set name", "TESTING", "TESTING", documentSetOptions);
IContentType newContentType = newDocumentSet.Parent as IContentType;
// Test the created object
Assert.IsNotNull(newContentType);
Assert.IsNotNull(newDocumentSet);

Assert.AreEqual(newDocumentSet.SharedColumns.Count, documentSetOptions.SharedColumns.Count);
Assert.AreEqual(newDocumentSet.WelcomePageColumns.Count, documentSetOptions.WelcomePageColumns.Count);
Assert.AreEqual(newDocumentSet.DefaultContents.Count, documentSetOptions.DefaultContents.Count);
Assert.AreEqual(newDocumentSet.AllowedContentTypes.Count, documentSetOptions.AllowedContentTypes.Count);
Assert.AreEqual(newDocumentSet.ShouldPrefixNameToFile, documentSetOptions.ShouldPrefixNameToFile);

await newContentType.DeleteAsync();
await file.DeleteAsync();
}
}

[TestMethod]
public async Task UpdateContentTypeAsDocumentSet()
{
//TestCommon.Instance.Mocking = false;
using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite))
{
var list = await context.Web.Lists.GetByTitle("Documents").GetAsync();
var rootFolder = await list.RootFolder.GetAsync();

(_, _, string documentUrl) = await TestAssets.CreateTestDocumentAsync(parentFolder: rootFolder);

var categoriesField = await context.Web.Fields.FirstAsync(y => y.InternalName == "Categories").ConfigureAwait(false);
var managersField = await context.Web.Fields.FirstAsync(y => y.InternalName == "ManagersName").ConfigureAwait(false);
var documentCt = await context.Web.ContentTypes.FirstAsync(y => y.Name == "Document").ConfigureAwait(false);

var file = await context.Web.GetFileByServerRelativeUrlAsync(documentUrl);

var documentSetOptions = new DocumentSetOptions
{
SharedColumns = new List<IField>
{
categoriesField
},
WelcomePageColumns = new List<IField>
{
categoriesField
},
DefaultContents = new List<DocumentSetContentOptions>
{
new DocumentSetContentOptions
{
FileName = "Test.docx",
FolderName = "FolderName",
File = file,
ContentTypeId = documentCt.StringId
}
}
};

IDocumentSet newDocumentSet = await context.Web.ContentTypes.AddDocumentSetAsync(docSetId, "Document Set Name", "TESTING", "TESTING", documentSetOptions);

Assert.IsNotNull(newDocumentSet);
Assert.AreEqual(newDocumentSet.SharedColumns.Count, documentSetOptions.SharedColumns.Count);
Assert.AreEqual(newDocumentSet.WelcomePageColumns.Count, documentSetOptions.WelcomePageColumns.Count);
Assert.AreEqual(newDocumentSet.DefaultContents.Count, documentSetOptions.DefaultContents.Count);

var documentSetOptionsUpdate = new DocumentSetOptions
{
SharedColumns = new List<IField>
{
managersField
},
WelcomePageColumns = new List<IField>
{
managersField
},
DefaultContents = new List<DocumentSetContentOptions>
{
new DocumentSetContentOptions
{
FileName = "Test2.docx",
FolderName = "FolderName2",
File = file,
ContentTypeId = documentCt.StringId
}
}
};
newDocumentSet = await newDocumentSet.UpdateAsync(documentSetOptionsUpdate);
IContentType newContentType = newDocumentSet.Parent as IContentType;

Assert.IsNotNull(newDocumentSet);
Assert.AreEqual(newDocumentSet.SharedColumns.Count, documentSetOptionsUpdate.SharedColumns.Count + documentSetOptions.SharedColumns.Count);
Assert.AreEqual(newDocumentSet.WelcomePageColumns.Count, documentSetOptionsUpdate.WelcomePageColumns.Count + documentSetOptions.WelcomePageColumns.Count);
Assert.AreEqual(newDocumentSet.DefaultContents.Count, documentSetOptionsUpdate.DefaultContents.Count + documentSetOptions.DefaultContents.Count);

await newContentType.DeleteAsync();
await file.DeleteAsync();
}
}

[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public async Task AddContentTypeAsDocumentSetExceptionAsyncTest()
{
//TestCommon.Instance.Mocking = false;
using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite))
{
IDocumentSet newDocumentSet = await context.Web.ContentTypes.AddDocumentSetAsync("0x0101mlalalala", "Document set name");
}
}

[TestMethod]
[ExpectedException(typeof(ClientException))]
public async Task GetContentTypeAsDocumentSetExceptionTest()
{
//TestCommon.Instance.Mocking = false;
using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite))
{
IContentType contentType = (from ct in context.Web.ContentTypes
where ct.StringId == "0x0101"
select ct)
.QueryProperties(ct => ct.StringId, ct => ct.Id)
.FirstOrDefault();
var documentSet = contentType.AsDocumentSet();
}
}

#endregion

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"IsSuccessStatusCode":true,"StatusCode":200,"Headers":{"SPRequestGuid":"bbdc42a0-c032-4000-4c6b-a2dff63a4ea3","SPClientServiceRequestDuration":"12","X-SharePointHealthScore":"2","X-SP-SERVERSTATE":"ReadOnly=0"},"Response":"{\u0022RegionalSettings\u0022:{\u0022TimeZone\u0022:{\u0022Description\u0022:\u0022(UTC-08:00) Pacific Time (US and Canada)\u0022,\u0022Id\u0022:13,\u0022Information\u0022:{\u0022Bias\u0022:480,\u0022DaylightBias\u0022:-60,\u0022StandardBias\u0022:0}},\u0022AdjustHijriDays\u0022:0,\u0022AlternateCalendarType\u0022:0,\u0022AM\u0022:\u0022AM\u0022,\u0022CalendarType\u0022:1,\u0022Collation\u0022:25,\u0022CollationLCID\u0022:2070,\u0022DateFormat\u0022:0,\u0022DateSeparator\u0022:\u0022/\u0022,\u0022DecimalSeparator\u0022:\u0022.\u0022,\u0022DigitGrouping\u0022:\u00223;0\u0022,\u0022FirstDayOfWeek\u0022:0,\u0022FirstWeekOfYear\u0022:0,\u0022IsEastAsia\u0022:false,\u0022IsRightToLeft\u0022:false,\u0022IsUIRightToLeft\u0022:false,\u0022ListSeparator\u0022:\u0022,\u0022,\u0022LocaleId\u0022:1033,\u0022NegativeSign\u0022:\u0022-\u0022,\u0022NegNumberMode\u0022:1,\u0022PM\u0022:\u0022PM\u0022,\u0022PositiveSign\u0022:\u0022\u0022,\u0022ShowWeeks\u0022:false,\u0022ThousandSeparator\u0022:\u0022,\u0022,\u0022Time24\u0022:false,\u0022TimeMarkerPosition\u0022:0,\u0022TimeSeparator\u0022:\u0022:\u0022,\u0022WorkDayEndHour\u0022:1020,\u0022WorkDays\u0022:62,\u0022WorkDayStartHour\u0022:480},\u0022Id\u0022:\u0022ceb5fd80-a434-4a62-a60b-b272f10ba1df\u0022,\u0022Url\u0022:\u0022https://mathijsdev2.sharepoint.com/sites/pnpcoresdktestgroup\u0022}"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"IsSuccessStatusCode":true,"StatusCode":200,"Headers":{"SPRequestGuid":"bbdc42a0-5037-4000-4c6b-ad656c469a01","SPClientServiceRequestDuration":"10","X-SharePointHealthScore":"2","X-SP-SERVERSTATE":"ReadOnly=0"},"Response":"{\u0022GroupId\u0022:\u00220ad55b5d-6a79-467b-ad21-d4bef7948a79\u0022,\u0022Id\u0022:\u00220f9b8f4f-0e8e-4630-bb0a-501442db9b64\u0022}"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"IsSuccessStatusCode":true,"StatusCode":200,"Headers":{"SPRequestGuid":"bbdc42a0-e03d-4000-4c6b-a42cc0dca2ca","SPClientServiceRequestDuration":"40","X-SharePointHealthScore":"3","X-SP-SERVERSTATE":"ReadOnly=0"},"Response":"{\u0022value\u0022:[{\u0022AllowContentTypes\u0022:true,\u0022BaseTemplate\u0022:101,\u0022BaseType\u0022:1,\u0022ContentTypesEnabled\u0022:false,\u0022CrawlNonDefaultViews\u0022:false,\u0022Created\u0022:\u00222022-03-06T01:58:24Z\u0022,\u0022CurrentChangeToken\u0022:{\u0022StringValue\u0022:\u00221;3;68e9c7c6-94e1-4422-9eef-ca5a274ee7c6;637896226038600000;496856759\u0022},\u0022DefaultContentApprovalWorkflowId\u0022:\u002200000000-0000-0000-0000-000000000000\u0022,\u0022DefaultItemOpenUseListSetting\u0022:false,\u0022Description\u0022:\u0022\u0022,\u0022Direction\u0022:\u0022none\u0022,\u0022DisableCommenting\u0022:false,\u0022DisableGridEditing\u0022:false,\u0022DocumentTemplateUrl\u0022:\u0022/sites/pnpcoresdktestgroup/Shared Documents/Forms/template.dotx\u0022,\u0022DraftVersionVisibility\u0022:0,\u0022EnableAttachments\u0022:false,\u0022EnableFolderCreation\u0022:true,\u0022EnableMinorVersions\u0022:false,\u0022EnableModeration\u0022:false,\u0022EnableRequestSignOff\u0022:true,\u0022EnableVersioning\u0022:true,\u0022EntityTypeName\u0022:\u0022Shared_x0020_Documents\u0022,\u0022ExemptFromBlockDownloadOfNonViewableFiles\u0022:false,\u0022FileSavePostProcessingEnabled\u0022:false,\u0022ForceCheckout\u0022:false,\u0022HasExternalDataSource\u0022:false,\u0022Hidden\u0022:false,\u0022Id\u0022:\u002268e9c7c6-94e1-4422-9eef-ca5a274ee7c6\u0022,\u0022ImagePath\u0022:{\u0022DecodedUrl\u0022:\u0022/_layouts/15/images/itdl.png?rev=47\u0022},\u0022ImageUrl\u0022:\u0022/_layouts/15/images/itdl.png?rev=47\u0022,\u0022DefaultSensitivityLabelForLibrary\u0022:\u0022\u0022,\u0022IrmEnabled\u0022:false,\u0022IrmExpire\u0022:false,\u0022IrmReject\u0022:false,\u0022IsApplicationList\u0022:false,\u0022IsCatalog\u0022:false,\u0022IsPrivate\u0022:false,\u0022ItemCount\u0022:4,\u0022LastItemDeletedDate\u0022:\u00222022-05-31T19:36:44Z\u0022,\u0022LastItemModifiedDate\u0022:\u00222022-05-31T19:36:44Z\u0022,\u0022LastItemUserModifiedDate\u0022:\u00222022-05-31T19:36:44Z\u0022,\u0022ListExperienceOptions\u0022:1,\u0022ListItemEntityTypeFullName\u0022:\u0022SP.Data.Shared_x0020_DocumentsItem\u0022,\u0022MajorVersionLimit\u0022:500,\u0022MajorWithMinorVersionsLimit\u0022:0,\u0022MultipleDataList\u0022:false,\u0022NoCrawl\u0022:false,\u0022ParentWebPath\u0022:{\u0022DecodedUrl\u0022:\u0022/sites/pnpcoresdktestgroup\u0022},\u0022ParentWebUrl\u0022:\u0022/sites/pnpcoresdktestgroup\u0022,\u0022ParserDisabled\u0022:false,\u0022ServerTemplateCanCreateFolders\u0022:true,\u0022TemplateFeatureId\u0022:\u002200bfea71-e717-4e80-aa17-d0c71b360101\u0022,\u0022Title\u0022:\u0022Documents\u0022}]}"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"IsSuccessStatusCode":true,"StatusCode":200,"Headers":{"SPRequestGuid":"bbdc42a0-b043-4000-4767-d130dc146c8d","SPClientServiceRequestDuration":"65","X-SharePointHealthScore":"2","X-SP-SERVERSTATE":"ReadOnly=0"},"Response":"{\u0022AllowContentTypes\u0022:true,\u0022BaseTemplate\u0022:101,\u0022BaseType\u0022:1,\u0022ContentTypesEnabled\u0022:false,\u0022CrawlNonDefaultViews\u0022:false,\u0022Created\u0022:\u00222022-03-06T01:58:24Z\u0022,\u0022CurrentChangeToken\u0022:{\u0022StringValue\u0022:\u00221;3;68e9c7c6-94e1-4422-9eef-ca5a274ee7c6;637896226049000000;496856760\u0022},\u0022DefaultContentApprovalWorkflowId\u0022:\u002200000000-0000-0000-0000-000000000000\u0022,\u0022DefaultItemOpenUseListSetting\u0022:false,\u0022Description\u0022:\u0022\u0022,\u0022Direction\u0022:\u0022none\u0022,\u0022DisableCommenting\u0022:false,\u0022DisableGridEditing\u0022:false,\u0022DocumentTemplateUrl\u0022:\u0022/sites/pnpcoresdktestgroup/Shared Documents/Forms/template.dotx\u0022,\u0022DraftVersionVisibility\u0022:0,\u0022EnableAttachments\u0022:false,\u0022EnableFolderCreation\u0022:true,\u0022EnableMinorVersions\u0022:false,\u0022EnableModeration\u0022:false,\u0022EnableRequestSignOff\u0022:true,\u0022EnableVersioning\u0022:true,\u0022EntityTypeName\u0022:\u0022Shared_x0020_Documents\u0022,\u0022ExemptFromBlockDownloadOfNonViewableFiles\u0022:false,\u0022FileSavePostProcessingEnabled\u0022:false,\u0022ForceCheckout\u0022:false,\u0022HasExternalDataSource\u0022:false,\u0022Hidden\u0022:false,\u0022Id\u0022:\u002268e9c7c6-94e1-4422-9eef-ca5a274ee7c6\u0022,\u0022ImagePath\u0022:{\u0022DecodedUrl\u0022:\u0022/_layouts/15/images/itdl.png?rev=47\u0022},\u0022ImageUrl\u0022:\u0022/_layouts/15/images/itdl.png?rev=47\u0022,\u0022DefaultSensitivityLabelForLibrary\u0022:\u0022\u0022,\u0022IrmEnabled\u0022:false,\u0022IrmExpire\u0022:false,\u0022IrmReject\u0022:false,\u0022IsApplicationList\u0022:false,\u0022IsCatalog\u0022:false,\u0022IsPrivate\u0022:false,\u0022ItemCount\u0022:4,\u0022LastItemDeletedDate\u0022:\u00222022-05-31T19:36:44Z\u0022,\u0022LastItemModifiedDate\u0022:\u00222022-05-31T19:36:44Z\u0022,\u0022LastItemUserModifiedDate\u0022:\u00222022-05-31T19:36:44Z\u0022,\u0022ListExperienceOptions\u0022:1,\u0022ListItemEntityTypeFullName\u0022:\u0022SP.Data.Shared_x0020_DocumentsItem\u0022,\u0022MajorVersionLimit\u0022:500,\u0022MajorWithMinorVersionsLimit\u0022:0,\u0022MultipleDataList\u0022:false,\u0022NoCrawl\u0022:false,\u0022ParentWebPath\u0022:{\u0022DecodedUrl\u0022:\u0022/sites/pnpcoresdktestgroup\u0022},\u0022ParentWebUrl\u0022:\u0022/sites/pnpcoresdktestgroup\u0022,\u0022ParserDisabled\u0022:false,\u0022ServerTemplateCanCreateFolders\u0022:true,\u0022TemplateFeatureId\u0022:\u002200bfea71-e717-4e80-aa17-d0c71b360101\u0022,\u0022Title\u0022:\u0022Documents\u0022}"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"IsSuccessStatusCode":true,"StatusCode":200,"Headers":{"SPRequestGuid":"bbdc42a0-204b-4000-4c6b-aa7f8aeabe27","SPClientServiceRequestDuration":"19","X-SharePointHealthScore":"3","X-SP-SERVERSTATE":"ReadOnly=0"},"Response":"{\u0022Exists\u0022:true,\u0022IsWOPIEnabled\u0022:false,\u0022ItemCount\u0022:4,\u0022Name\u0022:\u0022Shared Documents\u0022,\u0022ProgID\u0022:null,\u0022ServerRelativeUrl\u0022:\u0022/sites/pnpcoresdktestgroup/Shared Documents\u0022,\u0022TimeCreated\u0022:\u00222022-03-06T01:58:24Z\u0022,\u0022TimeLastModified\u0022:\u00222022-05-31T19:36:44Z\u0022,\u0022UniqueId\u0022:\u00222a0ba280-7c53-4eb9-a460-2c31576f90a8\u0022,\u0022WelcomePage\u0022:\u0022\u0022}"}
Loading