/// <summary> /// Converts the specified value back. /// </summary> /// <param name="value">The value.</param> /// <param name="arguments">The arguments.</param> /// <returns> /// The converted value. /// </returns> public object ConvertBack(object value, ConversionArguments arguments) { var imageValue = value as ImageValue; ImageFieldValue backConverted = null; if (imageValue != null) { backConverted = new ImageFieldValue() { Alignment = imageValue.Alignment, AlternateText = imageValue.AlternateText, BorderWidth = imageValue.BorderWidth, Height = imageValue.Height, HorizontalSpacing = imageValue.HorizontalSpacing, Hyperlink = imageValue.Hyperlink, ImageUrl = imageValue.ImageUrl, OpenHyperlinkInNewWindow = imageValue.OpenHyperlinkInNewWindow, VerticalSpacing = imageValue.VerticalSpacing, Width = imageValue.Width, }; } return backConverted; }
/// <summary> /// Reads a field value from a DataRow returned by a CAML query /// </summary> /// <param name="web">The context's web</param> /// <param name="dataRowFromCamlResult">The CAML-query-result data row we want to extract a field value from</param> /// <param name="fieldInternalName">The key to find the field among the data row cells</param> /// <returns>The value extracted from the data row's corresponding cell</returns> public override ImageValue ReadValueFromCamlResultDataRow(SPWeb web, DataRow dataRowFromCamlResult, string fieldInternalName) { var fieldValue = dataRowFromCamlResult[fieldInternalName]; if (fieldValue != null && fieldValue != System.DBNull.Value) { var imageFieldVal = new ImageFieldValue(fieldValue.ToString()); return(new ImageValue(imageFieldVal)); } return(null); }
/// <summary> /// Initializes a new instance of the <see cref="ImageValue"/> class. /// </summary> /// <param name="fieldImageValue">The field image value.</param> public ImageValue(ImageFieldValue fieldImageValue) { this.Alignment = fieldImageValue.Alignment; this.AlternateText = fieldImageValue.AlternateText; this.BorderWidth = fieldImageValue.BorderWidth; this.Height = fieldImageValue.Height; this.HorizontalSpacing = fieldImageValue.HorizontalSpacing; this.Hyperlink = fieldImageValue.Hyperlink; this.ImageUrl = fieldImageValue.ImageUrl; this.OpenHyperlinkInNewWindow = fieldImageValue.OpenHyperlinkInNewWindow; this.VerticalSpacing = fieldImageValue.VerticalSpacing; this.Width = fieldImageValue.Width; }
/// <summary> /// Writes an image field value to a SPListItem /// </summary> /// <param name="item">The SharePoint List Item</param> /// <param name="fieldValueInfo">The field and value information</param> public override void WriteValueToListItem(SPListItem item, FieldValueInfo fieldValueInfo) { var imageValue = fieldValueInfo.Value as ImageValue; ImageFieldValue sharePointFieldImageValue = null; if (imageValue != null) { sharePointFieldImageValue = CreateSharePointImageFieldValue(imageValue); } item[fieldValueInfo.FieldInfo.InternalName] = sharePointFieldImageValue; }
private static ImageFieldValue CreateSharePointImageFieldValue(ImageValue imageValue) { var fieldImageValue = new ImageFieldValue() { Alignment = imageValue.Alignment, AlternateText = imageValue.AlternateText, BorderWidth = imageValue.BorderWidth, Height = imageValue.Height, HorizontalSpacing = imageValue.HorizontalSpacing, Hyperlink = imageValue.Hyperlink, ImageUrl = imageValue.ImageUrl, OpenHyperlinkInNewWindow = imageValue.OpenHyperlinkInNewWindow, VerticalSpacing = imageValue.VerticalSpacing, Width = imageValue.Width, }; return(fieldImageValue); }
public void ImageValue_GivenAnImageFieldValueToImageValueConstructorExpectPropertiesToBeSetted() { // Arrange var sharePointFieldValue = new ImageFieldValue() { ImageUrl = NewImageUrl, Hyperlink = NewHyperlink, OpenHyperlinkInNewWindow = NewOpenHyperlinkInNewWindow, Alignment = NewAlignment, AlternateText = NewAlternateText, BorderWidth = NewBorderWidth, Height = NewHeight, Width = NewWidth, HorizontalSpacing = NewHorizontalSpacing, VerticalSpacing = NewVerticalSpacing }; // Act var imageValue = new ImageValue(sharePointFieldValue); // Assert Assert.AreEqual(NewImageUrl, imageValue.ImageUrl); Assert.AreEqual(NewHyperlink, imageValue.Hyperlink); Assert.AreEqual(NewOpenHyperlinkInNewWindow, imageValue.OpenHyperlinkInNewWindow); Assert.AreEqual(NewAlignment, imageValue.Alignment); Assert.AreEqual(NewAlternateText, imageValue.AlternateText); Assert.AreEqual(NewBorderWidth, imageValue.BorderWidth); Assert.AreEqual(NewHeight, imageValue.Height); Assert.AreEqual(NewWidth, imageValue.Width); Assert.AreEqual(NewHorizontalSpacing, imageValue.HorizontalSpacing); Assert.AreEqual(NewVerticalSpacing, imageValue.VerticalSpacing); }
/// <summary> /// Converts the specified value. /// </summary> /// <param name="value">The value.</param> /// <param name="arguments">The arguments.</param> /// <returns> /// The converted value. /// </returns> public object Convert(object value, ConversionArguments arguments) { var imageValue = value as ImageFieldValue; if (value == DBNull.Value) { return null; } if (imageValue == null) { var stringValue = value as string; if (!string.IsNullOrEmpty(stringValue)) { imageValue = new ImageFieldValue(stringValue); } } return imageValue != null ? new ImageValue(imageValue) : null; }
public void ToEntity_WhenMappingFromListItem_AndItemPropertiesAreFilledWithValues_ShouldMapEntityWithAllItemValues() { using (var testScope = SiteTestScope.BlankSite()) { // Arrange IntegerFieldInfo integerFieldInfo = new IntegerFieldInfo( "TestInternalNameInteger", new Guid("{12E262D0-C7C4-4671-A266-064CDBD3905A}"), "NameKeyInt", "DescriptionKeyInt", "GroupKey"); NumberFieldInfo numberFieldInfo = new NumberFieldInfo( "TestInternalNameNumber", new Guid("{5DD4EE0F-8498-4033-97D0-317A24988786}"), "NameKeyNumber", "DescriptionKeyNumber", "GroupKey"); CurrencyFieldInfo currencyFieldInfo = new CurrencyFieldInfo( "TestInternalNameCurrency", new Guid("{9E9963F6-1EE6-46FB-9599-783BBF4D6249}"), "NameKeyCurrency", "DescriptionKeyCurrency", "GroupKey"); BooleanFieldInfo boolFieldInfoBasic = new BooleanFieldInfo( "TestInternalNameBool", new Guid("{F556AB6B-9E51-43E2-99C9-4A4E551A4BEF}"), "NameKeyBool", "DescriptionKeyBool", "GroupKey"); BooleanFieldInfo boolFieldInfoDefaultTrue = new BooleanFieldInfo( "TestInternalNameBoolTrue", new Guid("{0D0289AD-C5FB-495B-96C6-48CC46737D08}"), "NameKeyBoolTrue", "DescriptionKeyBoolTrue", "GroupKey") { DefaultValue = true }; BooleanFieldInfo boolFieldInfoDefaultFalse = new BooleanFieldInfo( "TestInternalNameBoolFalse", new Guid("{628181BD-9B0B-4B7E-934F-1CF1796EA4E4}"), "NameKeyBoolFalse", "DescriptionKeyBoolFalse", "GroupKey") { DefaultValue = false }; DateTimeFieldInfo dateTimeFieldInfoFormula = new DateTimeFieldInfo( "TestInternalNameDateFormula", new Guid("{D23EAD73-9E18-46DB-A426-41B2D47F696C}"), "NameKeyDateTimeFormula", "DescriptionKeyDateTimeFormula", "GroupKey") { DefaultFormula = "=[Today]" }; DateTimeFieldInfo dateTimeFieldInfoDefault = new DateTimeFieldInfo( "TestInternalNameDateDefault", new Guid("{016BF8D9-CEDC-4BF4-BA21-AC6A8F174AD5}"), "NameKeyDateTimeDefault", "DescriptionKeyDateTimeDefault", "GroupKey"); TextFieldInfo textFieldInfo = new TextFieldInfo( "TestInternalNameText", new Guid("{0C58B4A1-B360-47FE-84F7-4D8F58AE80F6}"), "NameKey", "DescriptionKey", "GroupKey"); NoteFieldInfo noteFieldInfo = new NoteFieldInfo( "TestInternalNameNote", new Guid("{E315BB24-19C3-4F2E-AABC-9DE5EFC3D5C2}"), "NameKeyAlt", "DescriptionKeyAlt", "GroupKey"); HtmlFieldInfo htmlFieldInfo = new HtmlFieldInfo( "TestInternalNameHtml", new Guid("{D16958E7-CF9A-4C38-A8BB-99FC03BFD913}"), "NameKeyAlt", "DescriptionKeyAlt", "GroupKey"); ImageFieldInfo imageFieldInfo = new ImageFieldInfo( "TestInternalNameImage", new Guid("{6C5B9E77-B621-43AA-BFBF-B333093EFCAE}"), "NameKeyImage", "DescriptionKeyImage", "GroupKey"); UrlFieldInfo urlFieldInfo = new UrlFieldInfo( "TestInternalNameUrl", new Guid("{208F904C-5A1C-4E22-9A79-70B294FABFDA}"), "NameKeyUrl", "DescriptionKeyUrl", "GroupKey"); UrlFieldInfo urlFieldInfoImage = new UrlFieldInfo( "TestInternalNameUrlImg", new Guid("{96D22CFF-5B40-4675-B632-28567792E11B}"), "NameKeyUrlImg", "DescriptionKeyUrlImg", "GroupKey") { Format = UrlFieldFormat.Image }; LookupFieldInfo lookupFieldInfo = new LookupFieldInfo( "TestInternalNameLookup", new Guid("{62F8127C-4A8C-4217-8BD8-C6712753AFCE}"), "NameKey", "DescriptionKey", "GroupKey"); LookupFieldInfo lookupFieldInfoAlt = new LookupFieldInfo( "TestInternalNameLookupAlt", new Guid("{1F05DFFA-6396-4AEF-AD23-72217206D35E}"), "NameKey", "DescriptionKey", "GroupKey") { ShowField = "ID" }; LookupMultiFieldInfo lookupMultiFieldInfo = new LookupMultiFieldInfo( "TestInternalNameLookupM", new Guid("{2C9D4C0E-21EB-4742-8C6C-4C30DCD08A05}"), "NameKeyMulti", "DescriptionKeyMulti", "GroupKey"); var ensuredUser1 = testScope.SiteCollection.RootWeb.EnsureUser(Environment.UserDomainName + "\\" + Environment.UserName); var ensuredUser2 = testScope.SiteCollection.RootWeb.EnsureUser("OFFICE\\maxime.boissonneault"); UserFieldInfo userFieldInfo = new UserFieldInfo( "TestInternalNameUser", new Guid("{5B74DD50-0D2D-4D24-95AF-0C4B8AA3F68A}"), "NameKeyUser", "DescriptionKeyUser", "GroupKey"); UserMultiFieldInfo userMultiFieldInfo = new UserMultiFieldInfo( "TestInternalNameUserMulti", new Guid("{8C662588-D54E-4905-B232-856C2239B036}"), "NameKeyUserMulti", "DescriptionKeyUserMulti", "GroupKey"); MediaFieldInfo mediaFieldInfo = new MediaFieldInfo( "TestInternalNameMedia", new Guid("{A2F070FE-FE33-44FC-9FDF-D18E74ED4D67}"), "NameKeyMedia", "DescriptionKeyMEdia", "GroupKey"); var testTermSet = new TermSetInfo(Guid.NewGuid(), "Test Term Set"); // keep Ids random because, if this test fails midway, the term // set will not be cleaned up and upon next test run we will // run into a term set and term ID conflicts. var levelOneTermA = new TermInfo(Guid.NewGuid(), "Term A", testTermSet); var levelOneTermB = new TermInfo(Guid.NewGuid(), "Term B", testTermSet); var levelTwoTermAA = new TermInfo(Guid.NewGuid(), "Term A-A", testTermSet); var levelTwoTermAB = new TermInfo(Guid.NewGuid(), "Term A-B", testTermSet); TaxonomySession session = new TaxonomySession(testScope.SiteCollection); TermStore defaultSiteCollectionTermStore = session.DefaultSiteCollectionTermStore; Group defaultSiteCollectionGroup = defaultSiteCollectionTermStore.GetSiteCollectionGroup(testScope.SiteCollection); TermSet newTermSet = defaultSiteCollectionGroup.CreateTermSet(testTermSet.Label, testTermSet.Id); Term createdTermA = newTermSet.CreateTerm(levelOneTermA.Label, Language.English.Culture.LCID, levelOneTermA.Id); Term createdTermB = newTermSet.CreateTerm(levelOneTermB.Label, Language.English.Culture.LCID, levelOneTermB.Id); Term createdTermAA = createdTermA.CreateTerm(levelTwoTermAA.Label, Language.English.Culture.LCID, levelTwoTermAA.Id); Term createdTermAB = createdTermA.CreateTerm(levelTwoTermAB.Label, Language.English.Culture.LCID, levelTwoTermAB.Id); defaultSiteCollectionTermStore.CommitAll(); TaxonomyFieldInfo taxoFieldInfo = new TaxonomyFieldInfo( "TestInternalNameTaxo", new Guid("{18CC105F-16C9-43E2-9933-37F98452C038}"), "NameKey", "DescriptionKey", "GroupKey") { TermStoreMapping = new TaxonomyContext(testTermSet) // choices limited to all terms in test term set }; TaxonomyMultiFieldInfo taxoMultiFieldInfo = new TaxonomyMultiFieldInfo( "TestInternalNameTaxoMulti", new Guid("{2F49D362-B014-41BB-9959-1000C9A7FFA0}"), "NameKeyMulti", "DescriptionKey", "GroupKey") { TermStoreMapping = new TaxonomyContext(levelOneTermA) // choices limited to children of a specific term, instead of having full term set choices }; var fieldsToEnsure = new List<BaseFieldInfo>() { integerFieldInfo, numberFieldInfo, currencyFieldInfo, boolFieldInfoBasic, boolFieldInfoDefaultTrue, boolFieldInfoDefaultFalse, dateTimeFieldInfoFormula, dateTimeFieldInfoDefault, textFieldInfo, noteFieldInfo, htmlFieldInfo, imageFieldInfo, urlFieldInfo, urlFieldInfoImage, lookupFieldInfo, lookupFieldInfoAlt, lookupMultiFieldInfo, userFieldInfo, userMultiFieldInfo, mediaFieldInfo, taxoFieldInfo, taxoMultiFieldInfo }; ListInfo lookupListInfo = new ListInfo("sometestlistpathlookup", "DynamiteTestListNameKeyLookup", "DynamiteTestListDescriptionKeyLookup"); ListInfo listInfo = new ListInfo("sometestlistpath", "DynamiteTestListNameKey", "DynamiteTestListDescriptionKey") { FieldDefinitions = fieldsToEnsure }; // Note how we need to specify SPSite for injection context - ISharePointEntityBinder's implementation // is lifetime-scoped to InstancePerSite. using (var injectionScope = IntegrationTestServiceLocator.BeginLifetimeScope(testScope.SiteCollection)) { var listHelper = injectionScope.Resolve<IListHelper>(); // Lookup field ListId setup SPList lookupList = listHelper.EnsureList(testScope.SiteCollection.RootWeb, lookupListInfo); lookupFieldInfo.ListId = lookupList.ID; lookupFieldInfoAlt.ListId = lookupList.ID; lookupMultiFieldInfo.ListId = lookupList.ID; // Create the looked-up items var lookupItem1 = lookupList.Items.Add(); lookupItem1["Title"] = "Test Item 1"; lookupItem1.Update(); var lookupItem2 = lookupList.Items.Add(); lookupItem2["Title"] = "Test Item 2"; lookupItem2.Update(); // Create the first test list SPList list = listHelper.EnsureList(testScope.SiteCollection.RootWeb, listInfo); list.EnableVersioning = true; list.Update(); // Create item on list var itemOnList = list.AddItem(); // Update with the field values through the SharePoint API itemOnList["Title"] = "Item under test"; itemOnList["TestInternalNameInteger"] = 555; itemOnList["TestInternalNameNumber"] = 5.5; itemOnList["TestInternalNameCurrency"] = 500.95; itemOnList["TestInternalNameBool"] = true; itemOnList["TestInternalNameBoolTrue"] = false; itemOnList["TestInternalNameBoolFalse"] = true; itemOnList["TestInternalNameDateFormula"] = new DateTime(1977, 7, 7); itemOnList["TestInternalNameDateDefault"] = new DateTime(1977, 7, 7); itemOnList["TestInternalNameText"] = "Text value"; itemOnList["TestInternalNameNote"] = "Note value"; itemOnList["TestInternalNameHtml"] = "<p class=\"some-css-class\">HTML value</p>"; itemOnList["TestInternalNameImage"] = new ImageFieldValue() { Hyperlink = "http://github.com/GSoft-SharePoint/", ImageUrl = "/_layouts/15/MyFolder/MyImage.png" }; itemOnList["TestInternalNameUrl"] = new SPFieldUrlValue() { Url = "http://github.com/GSoft-SharePoint/", Description = "patate!" }; itemOnList["TestInternalNameUrlImg"] = new SPFieldUrlValue() { Url = "http://github.com/GSoft-SharePoint/", Description = "patate!" }; itemOnList["TestInternalNameLookup"] = new SPFieldLookupValue(1, "Test Item 1"); itemOnList["TestInternalNameLookupAlt"] = new SPFieldLookupValue(2, "2"); itemOnList["TestInternalNameLookupM"] = new SPFieldLookupValueCollection() { new SPFieldLookupValue(1, "Test Item 1"), new SPFieldLookupValue(2, "Test Item 2") }; itemOnList["TestInternalNameUser"] = new SPFieldUserValue(testScope.SiteCollection.RootWeb, ensuredUser1.ID, ensuredUser1.Name); itemOnList["TestInternalNameUserMulti"] = new SPFieldUserValueCollection() { new SPFieldUserValue(testScope.SiteCollection.RootWeb, ensuredUser1.ID, ensuredUser1.Name), new SPFieldUserValue(testScope.SiteCollection.RootWeb, ensuredUser2.ID, ensuredUser2.Name) }; itemOnList["TestInternalNameMedia"] = new MediaFieldValue() { Title = "Some media file title", MediaSource = "/sites/test/SiteAssets/01_01_ASP.NET%20MVC%203%20Fundamentals%20Intro%20-%20Overview.asf", AutoPlay = true, Loop = true, PreviewImageSource = "/_layouts/15/Images/logo.png" }; var taxonomyField = (TaxonomyField)itemOnList.Fields.GetFieldByInternalName("TestInternalNameTaxo"); taxonomyField.SetFieldValue(itemOnList, createdTermB); var taxonomyMultiField = (TaxonomyField)itemOnList.Fields.GetFieldByInternalName("TestInternalNameTaxoMulti"); taxonomyMultiField.SetFieldValue(itemOnList, new[] { createdTermAA, createdTermAB }); itemOnList.Update(); var entityBinder = injectionScope.Resolve<ISharePointEntityBinder>(); var entityMappedFromSingleItem = new TestItemEntityWithLookups(); var entityMappedFromItemVersion = new TestItemEntityWithLookups(); // Act // Map from SPListItem entityBinder.ToEntity<TestItemEntityWithLookups>(entityMappedFromSingleItem, itemOnList); // Map from SPListItemVersion entityBinder.ToEntity<TestItemEntityWithLookups>(entityMappedFromItemVersion, itemOnList.Versions[0]); // Map from DataRow/SPListItemCollection var entitiesMappedFromItemCollection = entityBinder.Get<TestItemEntity>(list.Items); // Assert // #1 Validate straight single list item to entity mappings Assert.AreEqual(555, entityMappedFromSingleItem.IntegerProperty); Assert.AreEqual(5.5, entityMappedFromSingleItem.DoubleProperty); Assert.AreEqual(500.95, entityMappedFromSingleItem.CurrencyProperty); Assert.IsTrue(entityMappedFromSingleItem.BoolProperty.Value); Assert.IsFalse(entityMappedFromSingleItem.BoolDefaultTrueProperty); Assert.IsTrue(entityMappedFromSingleItem.BoolDefaultFalseProperty); Assert.AreEqual(new DateTime(1977, 7, 7), entityMappedFromSingleItem.DateTimeFormulaProperty); Assert.AreEqual(new DateTime(1977, 7, 7), entityMappedFromSingleItem.DateTimeProperty); Assert.AreEqual("Text value", entityMappedFromSingleItem.TextProperty); Assert.AreEqual("Note value", entityMappedFromSingleItem.NoteProperty); Assert.AreEqual("<p class=\"some-css-class\">HTML value</p>", entityMappedFromSingleItem.HtmlProperty); Assert.IsNotNull(entityMappedFromSingleItem.ImageProperty); Assert.AreEqual("http://github.com/GSoft-SharePoint/", entityMappedFromSingleItem.ImageProperty.Hyperlink); Assert.AreEqual("/_layouts/15/MyFolder/MyImage.png", entityMappedFromSingleItem.ImageProperty.ImageUrl); Assert.AreEqual("http://github.com/GSoft-SharePoint/", entityMappedFromSingleItem.UrlProperty.Url); Assert.AreEqual("patate!", entityMappedFromSingleItem.UrlProperty.Description); Assert.AreEqual("http://github.com/GSoft-SharePoint/", entityMappedFromSingleItem.UrlImageProperty.Url); Assert.AreEqual("patate!", entityMappedFromSingleItem.UrlProperty.Description); Assert.AreEqual(1, entityMappedFromSingleItem.LookupProperty.Id); Assert.AreEqual("Test Item 1", entityMappedFromSingleItem.LookupProperty.Value); Assert.AreEqual(2, entityMappedFromSingleItem.LookupAltProperty.Id); Assert.AreEqual("2", entityMappedFromSingleItem.LookupAltProperty.Value); // ShowField/LookupField is ID Assert.AreEqual(1, entityMappedFromSingleItem.LookupMultiProperty[0].Id); Assert.AreEqual("Test Item 1", entityMappedFromSingleItem.LookupMultiProperty[0].Value); Assert.AreEqual(2, entityMappedFromSingleItem.LookupMultiProperty[1].Id); Assert.AreEqual("Test Item 2", entityMappedFromSingleItem.LookupMultiProperty[1].Value); Assert.AreEqual(ensuredUser1.Name, entityMappedFromSingleItem.UserProperty.DisplayName); Assert.AreEqual(ensuredUser1.Name, entityMappedFromSingleItem.UserMultiProperty[0].DisplayName); Assert.AreEqual("Maxime Boissonneault", entityMappedFromSingleItem.UserMultiProperty[1].DisplayName); Assert.AreEqual("Some media file title", entityMappedFromSingleItem.MediaProperty.Title); Assert.AreEqual(HttpUtility.UrlDecode("/sites/test/SiteAssets/01_01_ASP.NET%20MVC%203%20Fundamentals%20Intro%20-%20Overview.asf"), entityMappedFromSingleItem.MediaProperty.Url); Assert.IsTrue(entityMappedFromSingleItem.MediaProperty.IsAutoPlay); Assert.IsTrue(entityMappedFromSingleItem.MediaProperty.IsLoop); Assert.AreEqual("/_layouts/15/Images/logo.png", entityMappedFromSingleItem.MediaProperty.PreviewImageUrl); Assert.AreEqual(levelOneTermB.Id, entityMappedFromSingleItem.TaxonomyProperty.Id); Assert.AreEqual(levelOneTermB.Label, entityMappedFromSingleItem.TaxonomyProperty.Label); Assert.AreEqual(levelTwoTermAA.Id, entityMappedFromSingleItem.TaxonomyMultiProperty[0].Id); Assert.AreEqual(levelTwoTermAA.Label, entityMappedFromSingleItem.TaxonomyMultiProperty[0].Label); Assert.AreEqual(levelTwoTermAB.Id, entityMappedFromSingleItem.TaxonomyMultiProperty[1].Id); Assert.AreEqual(levelTwoTermAB.Label, entityMappedFromSingleItem.TaxonomyMultiProperty[1].Label); // #2 Validate list item version mappings Assert.AreEqual(555, entityMappedFromItemVersion.IntegerProperty); Assert.AreEqual(5.5, entityMappedFromItemVersion.DoubleProperty); Assert.AreEqual(500.95, entityMappedFromItemVersion.CurrencyProperty); Assert.IsTrue(entityMappedFromItemVersion.BoolProperty.Value); Assert.IsFalse(entityMappedFromItemVersion.BoolDefaultTrueProperty); Assert.IsTrue(entityMappedFromItemVersion.BoolDefaultFalseProperty); Assert.AreEqual(new DateTime(1977, 7, 7), entityMappedFromItemVersion.DateTimeFormulaProperty); Assert.AreEqual(new DateTime(1977, 7, 7), entityMappedFromItemVersion.DateTimeProperty); Assert.AreEqual("Text value", entityMappedFromItemVersion.TextProperty); Assert.AreEqual("Note value", entityMappedFromItemVersion.NoteProperty); Assert.AreEqual("<p class=\"some-css-class\">HTML value</p>", entityMappedFromItemVersion.HtmlProperty); Assert.IsNotNull(entityMappedFromItemVersion.ImageProperty); Assert.AreEqual("http://github.com/GSoft-SharePoint/", entityMappedFromItemVersion.ImageProperty.Hyperlink); Assert.AreEqual("/_layouts/15/MyFolder/MyImage.png", entityMappedFromItemVersion.ImageProperty.ImageUrl); Assert.AreEqual("http://github.com/GSoft-SharePoint/", entityMappedFromItemVersion.UrlProperty.Url); Assert.AreEqual("patate!", entityMappedFromItemVersion.UrlProperty.Description); Assert.AreEqual("http://github.com/GSoft-SharePoint/", entityMappedFromItemVersion.UrlImageProperty.Url); Assert.AreEqual("patate!", entityMappedFromItemVersion.UrlProperty.Description); Assert.AreEqual(1, entityMappedFromItemVersion.LookupProperty.Id); Assert.AreEqual("Test Item 1", entityMappedFromItemVersion.LookupProperty.Value); Assert.AreEqual(2, entityMappedFromItemVersion.LookupAltProperty.Id); Assert.AreEqual("2", entityMappedFromItemVersion.LookupAltProperty.Value); // ShowField/LookupField is ID Assert.AreEqual(1, entityMappedFromItemVersion.LookupMultiProperty[0].Id); Assert.AreEqual("Test Item 1", entityMappedFromItemVersion.LookupMultiProperty[0].Value); Assert.AreEqual(2, entityMappedFromItemVersion.LookupMultiProperty[1].Id); Assert.AreEqual("Test Item 2", entityMappedFromItemVersion.LookupMultiProperty[1].Value); Assert.AreEqual(ensuredUser1.Name, entityMappedFromItemVersion.UserProperty.DisplayName); Assert.AreEqual(ensuredUser1.Name, entityMappedFromItemVersion.UserMultiProperty[0].DisplayName); Assert.AreEqual("Maxime Boissonneault", entityMappedFromItemVersion.UserMultiProperty[1].DisplayName); Assert.AreEqual("Some media file title", entityMappedFromItemVersion.MediaProperty.Title); Assert.AreEqual(HttpUtility.UrlDecode("/sites/test/SiteAssets/01_01_ASP.NET%20MVC%203%20Fundamentals%20Intro%20-%20Overview.asf"), entityMappedFromItemVersion.MediaProperty.Url); Assert.IsTrue(entityMappedFromItemVersion.MediaProperty.IsAutoPlay); Assert.IsTrue(entityMappedFromItemVersion.MediaProperty.IsLoop); Assert.AreEqual("/_layouts/15/Images/logo.png", entityMappedFromItemVersion.MediaProperty.PreviewImageUrl); Assert.AreEqual(levelOneTermB.Id, entityMappedFromItemVersion.TaxonomyProperty.Id); Assert.AreEqual(levelOneTermB.Label, entityMappedFromItemVersion.TaxonomyProperty.Label); Assert.AreEqual(levelTwoTermAA.Id, entityMappedFromItemVersion.TaxonomyMultiProperty[0].Id); Assert.AreEqual(levelTwoTermAA.Label, entityMappedFromItemVersion.TaxonomyMultiProperty[0].Label); Assert.AreEqual(levelTwoTermAB.Id, entityMappedFromItemVersion.TaxonomyMultiProperty[1].Id); Assert.AreEqual(levelTwoTermAB.Label, entityMappedFromItemVersion.TaxonomyMultiProperty[1].Label); // #3 Validate straight list item collection to entity mappings Assert.AreEqual(555, entitiesMappedFromItemCollection[0].IntegerProperty); Assert.AreEqual(5.5, entitiesMappedFromItemCollection[0].DoubleProperty); Assert.AreEqual(500.95, entitiesMappedFromItemCollection[0].CurrencyProperty); Assert.IsTrue(entitiesMappedFromItemCollection[0].BoolProperty.Value); Assert.IsFalse(entitiesMappedFromItemCollection[0].BoolDefaultTrueProperty); Assert.IsTrue(entitiesMappedFromItemCollection[0].BoolDefaultFalseProperty); Assert.AreEqual(new DateTime(1977, 7, 7), entitiesMappedFromItemCollection[0].DateTimeFormulaProperty); Assert.AreEqual(new DateTime(1977, 7, 7), entitiesMappedFromItemCollection[0].DateTimeProperty); Assert.AreEqual("Text value", entitiesMappedFromItemCollection[0].TextProperty); Assert.AreEqual("Note value", entitiesMappedFromItemCollection[0].NoteProperty); Assert.AreEqual("<p class=\"some-css-class\">HTML value</p>", entitiesMappedFromItemCollection[0].HtmlProperty); Assert.IsNotNull(entitiesMappedFromItemCollection[0].ImageProperty); Assert.AreEqual("http://github.com/GSoft-SharePoint/", entitiesMappedFromItemCollection[0].ImageProperty.Hyperlink); Assert.AreEqual("/_layouts/15/MyFolder/MyImage.png", entitiesMappedFromItemCollection[0].ImageProperty.ImageUrl); Assert.AreEqual("http://github.com/GSoft-SharePoint/", entitiesMappedFromItemCollection[0].UrlProperty.Url); Assert.AreEqual("patate!", entitiesMappedFromItemCollection[0].UrlProperty.Description); Assert.AreEqual("http://github.com/GSoft-SharePoint/", entitiesMappedFromItemCollection[0].UrlImageProperty.Url); Assert.AreEqual("patate!", entitiesMappedFromItemCollection[0].UrlImageProperty.Description); // No lookups or User fields because DataRow formatting screws up lookup values (we lose the lookup IDs) Assert.AreEqual("Some media file title", entitiesMappedFromItemCollection[0].MediaProperty.Title); Assert.AreEqual(HttpUtility.UrlDecode("/sites/test/SiteAssets/01_01_ASP.NET%20MVC%203%20Fundamentals%20Intro%20-%20Overview.asf"), entitiesMappedFromItemCollection[0].MediaProperty.Url); Assert.IsTrue(entitiesMappedFromItemCollection[0].MediaProperty.IsAutoPlay); Assert.IsTrue(entitiesMappedFromItemCollection[0].MediaProperty.IsLoop); Assert.AreEqual("/_layouts/15/Images/logo.png", entitiesMappedFromItemCollection[0].MediaProperty.PreviewImageUrl); Assert.AreEqual(levelOneTermB.Id, entitiesMappedFromItemCollection[0].TaxonomyProperty.Id); Assert.AreEqual(levelOneTermB.Label, entitiesMappedFromItemCollection[0].TaxonomyProperty.Label); Assert.AreEqual(levelTwoTermAA.Id, entitiesMappedFromItemCollection[0].TaxonomyMultiProperty[0].Id); Assert.AreEqual(levelTwoTermAA.Label, entitiesMappedFromItemCollection[0].TaxonomyMultiProperty[0].Label); Assert.AreEqual(levelTwoTermAB.Id, entitiesMappedFromItemCollection[0].TaxonomyMultiProperty[1].Id); Assert.AreEqual(levelTwoTermAB.Label, entitiesMappedFromItemCollection[0].TaxonomyMultiProperty[1].Label); } // Cleanup term set so that we don't pollute the metadata store newTermSet.Delete(); defaultSiteCollectionTermStore.CommitAll(); } }
/// <summary> /// Scans through the fields in the list to check supported fields for broken links /// </summary> /// <param name="Item">The list item to check</param> /// <param name="PageUri">The full url of the page</param> private void ScanListFields(SPListItem Item, Uri PageUri) { foreach (SPField field in Item.Fields) { switch (field.TypeAsString) { case "HTML": case "Link": try { if ((Item[field.Title] != null) && !string.IsNullOrEmpty(Item[field.Title].ToString())) { StringCollection AllLinks = LinkCheckerUtilities.GetLinksFromHTML(Item[field.Title].ToString()); // Make all the links relative then validate foreach (string Url in AllLinks) { try { ValidateLink(PageUri, new Uri(PageUri, Url), Url, BrokenLinkType.HtmlField); } catch (Exception ex) { Utility.LogError(ex, string.Format("LinkChecker: Error checking link '{0}' at '{1}'", Url, PageUri.ToString())); } } } } catch (Exception ex) { Utility.LogError(ex, string.Format("LinkChecker: Error: Unable to check links for field {0} at {1} - Error Message: {2}, Stack Trace: {3}", field.Title, Item.Url, ex.Message, ex.StackTrace)); } break; case "Note": SPFieldMultiLineText fieldAsMultiLineText = (SPFieldMultiLineText)field; if ((fieldAsMultiLineText.RichText) && (Item[field.Title] != null) && !string.IsNullOrEmpty(Item[field.Title].ToString())) { StringCollection AllLinks = LinkCheckerUtilities.GetLinksFromHTML(Item[field.Title].ToString()); foreach (string Url in AllLinks) { try { ValidateLink(PageUri, new Uri(PageUri, Url), Url, BrokenLinkType.HtmlField); } catch (Exception ex) { Utility.LogError(ex, string.Format("LinkChecker: Error checking link '{0}' at '{1}'", Url, PageUri.ToString())); } } } break; case "URL": try { if ((Item[field.Title] != null) && !string.IsNullOrEmpty(Item[field.Title].ToString())) { SPFieldUrlValue UrlValue = new SPFieldUrlValue(Item[field.Title].ToString()); try { ValidateLink(PageUri, new Uri(UrlValue.Url), UrlValue.Url, BrokenLinkType.LinkField); } catch (Exception ex) { Utility.LogError(ex, string.Format("LinkChecker: Error checking link '{0}' at '{1}'", UrlValue.Url, PageUri.ToString())); } } } catch (Exception ex) { Utility.LogError(ex, string.Format("LinkChecker: Error: Unable to check links for field {0} at {1} - Error Message: {2}, Stack Trace: {3}", field.Title, Item.Url, ex.Message, ex.StackTrace)); } break; case "SPLink": try { if ((Item[field.Title] != null) && !string.IsNullOrEmpty(Item[field.Title].ToString())) { try { string Url = new LinkFieldValue(Item[field.Title].ToString()).NavigateUrl; Uri uri = new Uri(PageUri, Url); ValidateLink(PageUri, uri, Url, BrokenLinkType.LinkField); } catch (Exception ex) { Utility.LogError(ex, string.Format("LinkChecker: Error checking link '{0}' at '{1}'", Item[field.Title].ToString(), PageUri.ToString())); } } } catch (Exception ex) { Utility.LogError(ex, string.Format("LinkChecker: Error: Unable to check links for field {0} at {1} - Error Message: {2}, Stack Trace: {3}", field.Title, Item.Url, ex.Message, ex.StackTrace)); } break; case "Image": try { if ((Item[field.Title] != null) && !string.IsNullOrEmpty(Item[field.Title].ToString())) { ImageFieldValue ImgValue = new ImageFieldValue(Item[field.Title].ToString()); try { Uri uri = new Uri(PageUri, ImgValue.ImageUrl); ValidateLink(PageUri, uri, ImgValue.ImageUrl, BrokenLinkType.LinkField); } catch (Exception ex) { Utility.LogError(ex, string.Format("LinkChecker: Error checking image link '{0}' at '{1}'", ImgValue.ImageUrl, PageUri.ToString())); } } } catch (Exception ex) { Utility.LogError(ex, string.Format("LinkChecker: Error: Unable to check links for field {0} at {1} - Error Message: {2}, Stack Trace: {3}", field.Title, Item.Url, ex.Message, ex.StackTrace)); } break; case "SummaryLinks": try { if ((Item[field.Title] != null) && !string.IsNullOrEmpty(Item[field.Title].ToString())) { SummaryLinkFieldValue LinksFieldValue = new SummaryLinkFieldValue(Item[field.Title].ToString()); foreach (SummaryLink Link in LinksFieldValue.SummaryLinks) { try { ValidateLink(PageUri, new Uri(Link.LinkUrl), Link.LinkUrl, BrokenLinkType.SummaryLinkField); } catch (Exception ex) { Utility.LogError(ex, string.Format("LinkChecker: Error checking link '{0}' at '{1}'", Link.LinkUrl, PageUri.ToString())); } } } } catch (Exception ex) { Utility.LogError(ex, string.Format("LinkChecker: Error: Unable to check links for field {0} at {1} - Error Message: {2}, Stack Trace: {3}", field.Title, Item.Url, ex.Message, ex.StackTrace)); } break; } } }
/// <summary> /// CustomSaveHandler /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void CustomSaveHandler(object sender, EventArgs e) { SPContext.Current.Web.AllowUnsafeUpdates = true; var item = SPContext.Current.ListItem; SPFieldLookupValueCollection returnVal = new SPFieldLookupValueCollection(); foreach (ListItem lItem in ddlCat.Items) { if (lItem.Selected) { if (lItem.Value != "0" && lItem.Text != "(None)") { returnVal.Add((new SPFieldLookupValue( int.Parse(lItem.Value), lItem.Text))); } } } if (returnVal.Count > 0) { item[FieldsName.NewsRecord.English.CategoryName] = returnVal; } if (fuNewsImage.HasFile) { var webUrl = SPContext.Current.Web.ServerRelativeUrl; if (webUrl.Equals("/")) { webUrl = ""; } var fuThumbName = string.Format(CultureInfo.InvariantCulture, "{0}_{1}", Utilities.GetPreByTime(DateTime.Now), fuNewsImage.FileName); SPFile file = Utilities.UploadFileToDocumentLibrary(SPContext.Current.Web, fuNewsImage.PostedFile.InputStream, string.Format(CultureInfo.InvariantCulture, "{0}/{1}/{2}", webUrl, ListsName.English.ImagesList, fuThumbName)); //CurrentItem[FieldsName.NewsList.InternalName.ImageThumb] = file.Url; RichImageField rifImage = new RichImageField(); ImageFieldValue imageField = rifImage.Value as ImageFieldValue; if (imageField != null) { imageField.ImageUrl = webUrl + "/" + file.Url; item["PublishingPageImage"] = imageField; } } //Save item to list SaveButton.SaveItem(SPContext.Current, false, string.Empty); try { if (fuNewsImage.HasFile) { SPContext.Current.Web.AllowUnsafeUpdates = true; item.Attachments.Delete(fuNewsImage.FileName); SPContext.Current.Web.AllowUnsafeUpdates = true; item.SystemUpdate(false); } } catch (Exception ex) { Utilities.LogToUls(ex); } }