Skip to content

Include Webhook ClientState in Model #1096

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

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -3137,6 +3137,7 @@ public void XMLSerializer_Deserialize_ListInstances()
Assert.AreEqual("fake-template.stp", list.TemplateInternalName);
Assert.AreEqual(120, list.Webhooks[0].ExpiresInDays);
Assert.AreEqual("http://myapp.azurewebsites.net/WebHookListener", list.Webhooks[0].ServerNotificationUrl);
Assert.AreEqual("ABC", list.Webhooks[0].ClientState);

// security
var security = list.Security;
Expand Down Expand Up @@ -3413,7 +3414,8 @@ public void XMLSerializer_Serialize_ListInstances()
list.Webhooks.Add(new PnP.Framework.Provisioning.Model.Webhook
{
ExpiresInDays = 120,
ServerNotificationUrl = "http://myapp.azurewebsites.net/WebHookListener"
ServerNotificationUrl = "http://myapp.azurewebsites.net/WebHookListener",
ClientState = "CBA"
});

list.IRMSettings = new PnP.Framework.Provisioning.Model.IRMSettings
Expand Down Expand Up @@ -3668,6 +3670,7 @@ public void XMLSerializer_Serialize_ListInstances()
Assert.AreEqual("/Lists/ProjectDocuments", l.Url);
Assert.AreEqual(120, list.Webhooks[0].ExpiresInDays);
Assert.AreEqual("http://myapp.azurewebsites.net/WebHookListener", list.Webhooks[0].ServerNotificationUrl);
Assert.AreEqual("CBA", list.Webhooks[0].ClientState);

Assert.IsNotNull(l.Security);
var security = l.Security.BreakRoleInheritance;
Expand Down Expand Up @@ -4799,6 +4802,7 @@ public void XMLSerializer_Deserialize_SiteWebhooks()
Assert.AreEqual(120, webhooks[0].ExpiresInDays);
Assert.AreEqual(SiteWebhookType.WebCreated, webhooks[0].SiteWebhookType);
Assert.AreEqual("http://myapp.azurewebsites.net/WebHookListener", webhooks[0].ServerNotificationUrl);
Assert.AreEqual("DEF", webhooks[0].ClientState);
}

[TestMethod]
Expand All @@ -4813,7 +4817,8 @@ public void XMLSerializer_Serialize_SiteWebhooks()
{
SiteWebhookType = SiteWebhookType.WebCreated,
ServerNotificationUrl = "http://myapp.azurewebsites.net/WebHookListener",
ExpiresInDays = 120
ExpiresInDays = 120,
ClientState = "FED"
});

var serializer = new XMLPnPSchemaV201909Serializer();
Expand All @@ -4833,6 +4838,7 @@ public void XMLSerializer_Serialize_SiteWebhooks()
Assert.AreEqual("120", webhooks[0].ExpiresInDays);
Assert.AreEqual(SiteWebhookSiteWebhookType.WebCreated, webhooks[0].SiteWebhookType);
Assert.AreEqual("http://myapp.azurewebsites.net/WebHookListener", webhooks[0].ServerNotificationUrl);
Assert.AreEqual("FED", webhooks[0].ClientState);
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3238,6 +3238,7 @@ public void XMLSerializer_Deserialize_ListInstances()
Assert.AreEqual("fake-template.stp", list.TemplateInternalName);
Assert.AreEqual(120, list.Webhooks[0].ExpiresInDays);
Assert.AreEqual("http://myapp.azurewebsites.net/WebHookListener", list.Webhooks[0].ServerNotificationUrl);
Assert.AreEqual("ABC", list.Webhooks[0].ClientState);

// security
var security = list.Security;
Expand Down Expand Up @@ -3514,7 +3515,8 @@ public void XMLSerializer_Serialize_ListInstances()
list.Webhooks.Add(new PnP.Framework.Provisioning.Model.Webhook
{
ExpiresInDays = 120,
ServerNotificationUrl = "http://myapp.azurewebsites.net/WebHookListener"
ServerNotificationUrl = "http://myapp.azurewebsites.net/WebHookListener",
ClientState = "CBA"
});

list.IRMSettings = new PnP.Framework.Provisioning.Model.IRMSettings
Expand Down Expand Up @@ -3769,6 +3771,7 @@ public void XMLSerializer_Serialize_ListInstances()
Assert.AreEqual("/Lists/ProjectDocuments", l.Url);
Assert.AreEqual(120, list.Webhooks[0].ExpiresInDays);
Assert.AreEqual("http://myapp.azurewebsites.net/WebHookListener", list.Webhooks[0].ServerNotificationUrl);
Assert.AreEqual("CBA", list.Webhooks[0].ClientState);

Assert.IsNotNull(l.Security);
var security = l.Security.BreakRoleInheritance;
Expand Down Expand Up @@ -4900,6 +4903,7 @@ public void XMLSerializer_Deserialize_SiteWebhooks()
Assert.AreEqual(120, webhooks[0].ExpiresInDays);
Assert.AreEqual(SiteWebhookType.WebCreated, webhooks[0].SiteWebhookType);
Assert.AreEqual("http://myapp.azurewebsites.net/WebHookListener", webhooks[0].ServerNotificationUrl);
Assert.AreEqual("DEF", webhooks[0].ClientState);
}

[TestMethod]
Expand All @@ -4914,7 +4918,8 @@ public void XMLSerializer_Serialize_SiteWebhooks()
{
SiteWebhookType = SiteWebhookType.WebCreated,
ServerNotificationUrl = "http://myapp.azurewebsites.net/WebHookListener",
ExpiresInDays = 120
ExpiresInDays = 120,
ClientState = "FED"
});

var serializer = new XMLPnPSchemaV202002Serializer();
Expand All @@ -4934,6 +4939,7 @@ public void XMLSerializer_Serialize_SiteWebhooks()
Assert.AreEqual("120", webhooks[0].ExpiresInDays);
Assert.AreEqual(SiteWebhookSiteWebhookType.WebCreated, webhooks[0].SiteWebhookType);
Assert.AreEqual("http://myapp.azurewebsites.net/WebHookListener", webhooks[0].ServerNotificationUrl);
Assert.AreEqual("FED", webhooks[0].ClientState);
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3403,6 +3403,7 @@ public void XMLSerializer_Deserialize_ListInstances()
Assert.AreEqual("fake-template.stp", list.TemplateInternalName);
Assert.AreEqual(120, list.Webhooks[0].ExpiresInDays);
Assert.AreEqual("http://myapp.azurewebsites.net/WebHookListener", list.Webhooks[0].ServerNotificationUrl);
Assert.AreEqual("ABC", list.Webhooks[0].ClientState);

// security
var security = list.Security;
Expand Down Expand Up @@ -3687,7 +3688,8 @@ public void XMLSerializer_Serialize_ListInstances()
list.Webhooks.Add(new PnP.Framework.Provisioning.Model.Webhook
{
ExpiresInDays = 120,
ServerNotificationUrl = "http://myapp.azurewebsites.net/WebHookListener"
ServerNotificationUrl = "http://myapp.azurewebsites.net/WebHookListener",
ClientState = "CBA"
});

list.IRMSettings = new PnP.Framework.Provisioning.Model.IRMSettings
Expand Down Expand Up @@ -3942,6 +3944,7 @@ public void XMLSerializer_Serialize_ListInstances()
Assert.AreEqual("/Lists/ProjectDocuments", l.Url);
Assert.AreEqual(120, list.Webhooks[0].ExpiresInDays);
Assert.AreEqual("http://myapp.azurewebsites.net/WebHookListener", list.Webhooks[0].ServerNotificationUrl);
Assert.AreEqual("CBA", list.Webhooks[0].ClientState);

Assert.IsNotNull(l.Security);
var security = l.Security.BreakRoleInheritance;
Expand Down Expand Up @@ -5084,6 +5087,7 @@ public void XMLSerializer_Deserialize_SiteWebhooks()
Assert.AreEqual(120, webhooks[0].ExpiresInDays);
Assert.AreEqual(SiteWebhookType.WebCreated, webhooks[0].SiteWebhookType);
Assert.AreEqual("http://myapp.azurewebsites.net/WebHookListener", webhooks[0].ServerNotificationUrl);
Assert.AreEqual("DEF", webhooks[0].ClientState);
}

[TestMethod]
Expand All @@ -5098,7 +5102,8 @@ public void XMLSerializer_Serialize_SiteWebhooks()
{
SiteWebhookType = SiteWebhookType.WebCreated,
ServerNotificationUrl = "http://myapp.azurewebsites.net/WebHookListener",
ExpiresInDays = 120
ExpiresInDays = 120,
ClientState = "FED"
});

var serializer = new XMLPnPSchemaV202103Serializer();
Expand All @@ -5118,6 +5123,7 @@ public void XMLSerializer_Serialize_SiteWebhooks()
Assert.AreEqual("120", webhooks[0].ExpiresInDays);
Assert.AreEqual(SiteWebhookSiteWebhookType.WebCreated, webhooks[0].SiteWebhookType);
Assert.AreEqual("http://myapp.azurewebsites.net/WebHookListener", webhooks[0].ServerNotificationUrl);
Assert.AreEqual("FED", webhooks[0].ClientState);
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3454,6 +3454,7 @@ public void XMLSerializer_Deserialize_ListInstances()
Assert.IsTrue(list.EnableClassicAudienceTargeting);
Assert.AreEqual(120, list.Webhooks[0].ExpiresInDays);
Assert.AreEqual("http://myapp.azurewebsites.net/WebHookListener", list.Webhooks[0].ServerNotificationUrl);
Assert.AreEqual("ABC", list.Webhooks[0].ClientState);

// security
var security = list.Security;
Expand Down Expand Up @@ -3754,7 +3755,8 @@ public void XMLSerializer_Serialize_ListInstances()
list.Webhooks.Add(new PnP.Framework.Provisioning.Model.Webhook
{
ExpiresInDays = 120,
ServerNotificationUrl = "http://myapp.azurewebsites.net/WebHookListener"
ServerNotificationUrl = "http://myapp.azurewebsites.net/WebHookListener",
ClientState = "CBA"
});

list.IRMSettings = new PnP.Framework.Provisioning.Model.IRMSettings
Expand Down Expand Up @@ -4026,6 +4028,7 @@ public void XMLSerializer_Serialize_ListInstances()

Assert.AreEqual(120, list.Webhooks[0].ExpiresInDays);
Assert.AreEqual("http://myapp.azurewebsites.net/WebHookListener", list.Webhooks[0].ServerNotificationUrl);
Assert.AreEqual("CBA", list.Webhooks[0].ClientState);

Assert.IsNotNull(l.Security);
var security = l.Security.BreakRoleInheritance;
Expand Down Expand Up @@ -5168,6 +5171,7 @@ public void XMLSerializer_Deserialize_SiteWebhooks()
Assert.AreEqual(120, webhooks[0].ExpiresInDays);
Assert.AreEqual(SiteWebhookType.WebCreated, webhooks[0].SiteWebhookType);
Assert.AreEqual("http://myapp.azurewebsites.net/WebHookListener", webhooks[0].ServerNotificationUrl);
Assert.AreEqual("DEF", webhooks[0].ClientState);
}

[TestMethod]
Expand All @@ -5182,7 +5186,8 @@ public void XMLSerializer_Serialize_SiteWebhooks()
{
SiteWebhookType = SiteWebhookType.WebCreated,
ServerNotificationUrl = "http://myapp.azurewebsites.net/WebHookListener",
ExpiresInDays = 120
ExpiresInDays = 120,
ClientState = "FED"
});

var serializer = new TargetSerializer();
Expand All @@ -5202,6 +5207,7 @@ public void XMLSerializer_Serialize_SiteWebhooks()
Assert.AreEqual("120", webhooks[0].ExpiresInDays);
Assert.AreEqual(SiteWebhookSiteWebhookType.WebCreated, webhooks[0].SiteWebhookType);
Assert.AreEqual("http://myapp.azurewebsites.net/WebHookListener", webhooks[0].ServerNotificationUrl);
Assert.AreEqual("FED", webhooks[0].ClientState);
}

[TestMethod]
Expand Down
17 changes: 10 additions & 7 deletions src/lib/PnP.Framework/Extensions/ListExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,9 @@ public static WebhookSubscription AddWebhookSubscription(this List list, string
/// <param name="webHookEndPoint">Url of the web hook service endpoint (the one that will be called during an event)</param>
/// <param name="expirationDateTime">New web hook expiration date</param>
/// <param name="accessToken">(optional) The access token to SharePoint</param>
/// <param name="clientState">(optional) New web hook client state</param>
/// <returns><c>true</c> if the update succeeded, <c>false</c> otherwise</returns>
public static bool UpdateWebhookSubscription(this List list, string subscriptionId, string webHookEndPoint, DateTime expirationDateTime, string accessToken = null)
public static bool UpdateWebhookSubscription(this List list, string subscriptionId, string webHookEndPoint, DateTime expirationDateTime, string accessToken = null, string clientState= null)
{
// Get the access from the client context if not specified.
accessToken = accessToken ?? list.Context.GetAccessToken();
Expand All @@ -233,7 +234,7 @@ public static bool UpdateWebhookSubscription(this List list, string subscription

try
{
return WebhookUtility.UpdateWebhookSubscriptionAsync(list.Context.Url, WebHookResourceType.List, listId.ToString(), subscriptionId, webHookEndPoint, expirationDateTime, accessToken, list.Context as ClientContext).Result;
return WebhookUtility.UpdateWebhookSubscriptionAsync(list.Context.Url, WebHookResourceType.List, listId.ToString(), subscriptionId, webHookEndPoint, expirationDateTime, accessToken, list.Context as ClientContext, clientState).Result;
}
catch (AggregateException ex)
{
Expand All @@ -250,10 +251,11 @@ public static bool UpdateWebhookSubscription(this List list, string subscription
/// <param name="subscriptionId">The id of the subscription to remove</param>
/// <param name="expirationDateTime">New web hook expiration date</param>
/// <param name="accessToken">(optional) The access token to SharePoint</param>
/// <param name="clientState">(optional) New web hook client state</param>
/// <returns><c>true</c> if the update succeeded, <c>false</c> otherwise</returns>
public static bool UpdateWebhookSubscription(this List list, Guid subscriptionId, DateTime expirationDateTime, string accessToken = null)
public static bool UpdateWebhookSubscription(this List list, Guid subscriptionId, DateTime expirationDateTime, string accessToken = null, string clientState = null)
{
return UpdateWebhookSubscription(list, subscriptionId.ToString(), null, expirationDateTime, accessToken);
return UpdateWebhookSubscription(list, subscriptionId.ToString(), null, expirationDateTime, accessToken, clientState);
}

/// <summary>
Expand All @@ -265,10 +267,11 @@ public static bool UpdateWebhookSubscription(this List list, Guid subscriptionId
/// <param name="webHookEndPoint">Url of the web hook service endpoint (the one that will be called during an event)</param>
/// <param name="expirationDateTime">New web hook expiration date</param>
/// <param name="accessToken">(optional) The access token to SharePoint</param>
/// <param name="clientState">(optional) New web hook client state</param>
/// <returns><c>true</c> if the update succeeded, <c>false</c> otherwise</returns>
public static bool UpdateWebhookSubscription(this List list, Guid subscriptionId, string webHookEndPoint, DateTime expirationDateTime, string accessToken = null)
public static bool UpdateWebhookSubscription(this List list, Guid subscriptionId, string webHookEndPoint, DateTime expirationDateTime, string accessToken = null, string clientState = null)
{
return UpdateWebhookSubscription(list, subscriptionId.ToString(), webHookEndPoint, expirationDateTime, accessToken);
return UpdateWebhookSubscription(list, subscriptionId.ToString(), webHookEndPoint, expirationDateTime, accessToken, clientState);
}

/// <summary>
Expand All @@ -281,7 +284,7 @@ public static bool UpdateWebhookSubscription(this List list, Guid subscriptionId
/// <returns><c>true</c> if the update succeeded, <c>false</c> otherwise</returns>
public static bool UpdateWebhookSubscription(this List list, WebhookSubscription subscription, string accessToken = null)
{
return UpdateWebhookSubscription(list, subscription.Id, subscription.NotificationUrl, subscription.ExpirationDateTime, accessToken);
return UpdateWebhookSubscription(list, subscription.Id, subscription.NotificationUrl, subscription.ExpirationDateTime, accessToken, subscription.ClientState);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ public partial class SiteWebhook : Webhook
/// <returns>Returns HashCode</returns>
public override int GetHashCode()
{
return (String.Format("{0}|{1}|{2}|",
return (String.Format("{0}|{1}|{2}|{3}|",
ServerNotificationUrl?.GetHashCode() ?? 0,
ExpiresInDays.GetHashCode(),
ClientState?.GetHashCode() ?? 0,
SiteWebhookType.GetHashCode()
).GetHashCode());
}
Expand All @@ -57,7 +58,7 @@ public override bool Equals(object obj)
}

/// <summary>
/// Compares SiteWebhook object based on ServerNotificationUrl, ExpiresInDays, and SiteWebhookType
/// Compares SiteWebhook object based on ServerNotificationUrl, ExpiresInDays, ClientState and SiteWebhookType
/// </summary>
/// <param name="other">SiteWebhook Class object</param>
/// <returns>true if the SiteWebhook object is equal to the current object; otherwise, false.</returns>
Expand All @@ -70,6 +71,7 @@ public bool Equals(SiteWebhook other)

return (this.ServerNotificationUrl == other.ServerNotificationUrl &&
this.ExpiresInDays == other.ExpiresInDays &&
this.ClientState == other.ClientState &&
this.SiteWebhookType == other.SiteWebhookType
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public partial class Webhook : BaseModel, IEquatable<Webhook>
/// </remarks>
public Int32 ExpiresInDays { get; set; }

/// <summary>
/// Defines an opaque string passed back to the client on all notifications, optional attribute.
/// </summary>
public String ClientState { get; set; }

#endregion

#region Comparison code
Expand All @@ -32,9 +37,10 @@ public partial class Webhook : BaseModel, IEquatable<Webhook>
/// <returns>Returns HashCode</returns>
public override int GetHashCode()
{
return (String.Format("{0}|{1}|",
return (String.Format("{0}|{1}|{2}|",
ServerNotificationUrl?.GetHashCode() ?? 0,
ExpiresInDays.GetHashCode()
ExpiresInDays.GetHashCode(),
ClientState?.GetHashCode() ?? 0
).GetHashCode());
}

Expand All @@ -53,7 +59,7 @@ public override bool Equals(object obj)
}

/// <summary>
/// Compares Webhook object based on ServerNotificationUrl and ExpiresInDays
/// Compares Webhook object based on ServerNotificationUrl, ExpiresInDays and ClientState
/// </summary>
/// <param name="other">Webhook Class object</param>
/// <returns>true if the Webhook object is equal to the current object; otherwise, false.</returns>
Expand All @@ -65,7 +71,8 @@ public bool Equals(Webhook other)
}

return (this.ServerNotificationUrl == other.ServerNotificationUrl &&
this.ExpiresInDays == other.ExpiresInDays
this.ExpiresInDays == other.ExpiresInDays &&
this.ClientState == other.ClientState
);
}

Expand Down
Loading