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

Append UndoCheckOutFile extension #842

Merged
merged 1 commit into from
Feb 22, 2023
Merged
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
46 changes: 46 additions & 0 deletions src/lib/PnP.Framework/Extensions/FileFolderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,52 @@ private static async Task CheckOutFileImplementation(this Web web, string server
}
}

/// <summary>
/// Discard changes to a file
/// </summary>
/// <param name="web">The web to process</param>
/// <param name="serverRelativeUrl">The server relative URL of the file to dicard changes</param>
public static void UndoCheckOutFile(this Web web, string serverRelativeUrl)
{
Task.Run(() => web.UndoCheckOutFileImplementation(serverRelativeUrl)).GetAwaiter().GetResult();
}

/// <summary>
/// Discard changes to a file
/// </summary>
/// <param name="web">The web to process</param>
/// <param name="serverRelativeUrl">The server relative URL of the file to dicard changes</param>
public static async Task UndoCheckOutFileAsync(this Web web, string serverRelativeUrl)
{
await new SynchronizationContextRemover();
await web.UndoCheckOutFileImplementation(serverRelativeUrl);
}

/// <summary>
/// Discard changes to a file
/// </summary>
/// <param name="web">The web to process</param>
/// <param name="serverRelativeUrl">The server relative URL of the file to dicard changes</param>
private static async Task UndoCheckOutFileImplementation(this Web web, string serverRelativeUrl)
{
var file = web.GetFileByServerRelativePath(ResourcePath.FromDecodedUrl(serverRelativeUrl));
await web.Context.ExecuteQueryAsync();

var scope = new ConditionalScope(web.Context, () => !file.ServerObjectIsNull.Value && file.Exists && file.CheckOutType != CheckOutType.None);

using (scope.StartScope())
{
web.Context.Load(file);
}
await web.Context.ExecuteQueryAsync();

if (scope.TestResult.Value)
{
file.UndoCheckOut();
web.Context.ExecuteQueryRetry();
}
}

private static void CopyStream(Stream source, Stream destination)
{
byte[] buffer = new byte[32768];
Expand Down