Skip to content

Commit

Permalink
#15 Taxonomy
Browse files Browse the repository at this point in the history
  • Loading branch information
OndrejValenta committed Jul 9, 2018
1 parent 33deca8 commit 712b048
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 90 deletions.
16 changes: 8 additions & 8 deletions FluentlySharepoint/FluentlySharePoint/CSOMOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public class CSOMOperation : IDisposable

public Guid CorrelationId { get; set; }
private ILogger Logger { get; set; } = new BlackHoleLogger();
public Func<Guid, string, string> LogMessageFormat { get; set; } =
(correlationId, message) => $"{(correlationId != Guid.Empty ? $"{correlationId}: " : "")}{message}";
public Func<Guid, string, string> LogMessageFormat { get; set; } =
(correlationId, message) => $"{(correlationId != Guid.Empty ? $"{correlationId}: " : "")}{message}";

public Dictionary<string, Site> LoadedSites { get; } = new Dictionary<string, Site>(5);
public Dictionary<string, Web> LoadedWebs { get; } = new Dictionary<string, Web>(5);
Expand All @@ -38,15 +38,15 @@ public class CSOMOperation : IDisposable
public ContentType LastContentType { get; private set; }

public TaxonomyOperation TaxonomyOperation { get; set; }

public CSOMOperation(ClientContext context) : this(context, null)
{
}

public CSOMOperation(ClientContext context, ILogger logger = null)
{
Context = context;
Logger = logger??Logger;
Logger = logger ?? Logger;

setupOperation(Context);
}
Expand Down Expand Up @@ -169,13 +169,13 @@ private void ProcessLoaded(ClientObject clientObject)
switch (clientObject)
{
case Web w:
LoadedWebs[w.ServerRelativeUrl] = w;
LoadedWebs.AddOrUpdate(w.ServerRelativeUrl, w);
break;
case Site s:
LoadedSites[s.ServerRelativeUrl] = s;
LoadedSites.AddOrUpdate(s.ServerRelativeUrl, s);
break;
case List l:
LoadedLists[l.Title] = l;
LoadedLists.AddOrUpdate(l.Title, l);
break;
case WebCollection wc:
wc.ToList().ForEach(ProcessLoaded);
Expand All @@ -192,7 +192,7 @@ public CSOMOperation SetLogMessageFormat(Func<Guid, string, string> logMessageFo
return this;
}

public CSOMOperation Load<T>(T clientObject, params Expression<Func<T, object>>[] retrievals) where T: ClientObject
public CSOMOperation Load<T>(T clientObject, params Expression<Func<T, object>>[] retrievals) where T : ClientObject
{
Context.Load(clientObject, retrievals);

Expand Down
52 changes: 49 additions & 3 deletions FluentlySharepoint/FluentlySharePoint/Extensions/Taxonomy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static class DefaultRetrievals

private static void EnsureTaxonomyOperation(CSOMOperation operation)
{
operation.TaxonomyOperation = operation.TaxonomyOperation ?? new TaxonomyOperation();
operation.TaxonomyOperation = operation.TaxonomyOperation ?? new TaxonomyOperation(operation);
}

public static CSOMOperation LockTerm(this CSOMOperation operation)
Expand All @@ -57,8 +57,7 @@ public static CSOMOperation LockTerm(this CSOMOperation operation)

public static CSOMOperation OpenTaxonomySession(this CSOMOperation operation)
{
if(operation.TaxonomyOperation == null)
operation.TaxonomyOperation = new TaxonomyOperation();
EnsureTaxonomyOperation(operation);

operation.TaxonomyOperation.Session = TaxonomySession.GetTaxonomySession(operation.Context);
operation.Context.Load(operation.TaxonomyOperation.Session);
Expand Down Expand Up @@ -196,5 +195,52 @@ public static CSOMOperation LoadTermSet(this CSOMOperation operation, string nam

return operation;
}

public static CSOMOperation LoadTerm(this CSOMOperation operation, string name, params Expression<Func<TermSet, object>>[] retrievals)
{
var op = operation.TaxonomyOperation;
var key = name.ToLower();
op.LastTerm = op.LastTermSet.Terms.GetByName(name);
operation.Load(op.LastTermSet, retrievals.Length == 0 ? DefaultRetrievals.TermSet : retrievals);

if (op.LoadedTermSets.ContainsKey(key))
{
op.LoadedTermSets[key] = op.LastTermSet;
}
else
{
op.LoadedTermSets.Add(key, op.LastTermSet);
}

return operation;
}

//public static CSOMOperation GetTermSets(this CSOMOperation operation, string name, params Expression<Func<TermSet, object>>[] retrievals)
//{
// var op = operation.TaxonomyOperation;

// op.Session.GetTermSetsByName(new LabelMatchInformation(operation.Context)
// {
// StringMatchOption = StringMatchOption.StartsWith,
// TermLabel = name,

// });

// var key = name.ToLower();
// op.LastTermSet = op.LastTermGroup.TermSets.GetByName(name);
// operation.Load(op.LastTermSet, retrievals.Length == 0 ? DefaultRetrievals.TermSet : retrievals);

// if (op.LoadedTermSets.ContainsKey(key))
// {
// op.LoadedTermSets[key] = op.LastTermSet;
// }
// else
// {
// op.LoadedTermSets.Add(key, op.LastTermSet);
// }

// return operation;
//}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SharePointOnline.CSOM" Version="16.1.7414.1200" />
<PackageReference Include="Microsoft.SharePointOnline.CSOM" Version="16.1.7813.1200" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SharePointOnline.CSOM" Version="16.1.7414.1200" />
<PackageReference Include="Microsoft.SharePointOnline.CSOM" Version="16.1.7813.1200" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Collections.Generic;

namespace KeenMate.FluentlySharePoint
{
public static class DictionaryExtensions
{
public static void AddOrUpdate<Tkey, Tvalue>(this Dictionary<Tkey, Tvalue> dictionary, Tkey key, Tvalue value)
{
if (dictionary.ContainsKey(key))
{
dictionary[key] = value;
}
else
{
dictionary.Add(key, value);
}
}
}
}
65 changes: 65 additions & 0 deletions FluentlySharepoint/FluentlySharePoint/Loggers/ConsoleLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using KeenMate.FluentlySharePoint.Interfaces;

namespace KeenMate.FluentlySharePoint.Loggers
{
/// <summary>
/// Dummy console logger
/// </summary>
public class ConsoleLogger : ILogger
{
public static string DateTimeFormat = "yyyy-MM-dd HH:mm:ss.ffff";

public void WriteLineInColor(string message, ConsoleColor color)
{
Console.ForegroundColor = color;
Console.WriteLine($"{DateTime.Now.ToString(DateTimeFormat)} - {message}");
Console.ResetColor();
}

public void Trace(string message)
{
WriteLineInColor(message, ConsoleColor.Gray);
}

public void Debug(string message)
{
WriteLineInColor(message, ConsoleColor.DarkGray);
}

public void Info(string message)
{
WriteLineInColor(message, ConsoleColor.Cyan);
}

public void Warn(string message)
{
WriteLineInColor(message, ConsoleColor.Yellow);
}

public void Warn(Exception ex, string message)
{
WriteLineInColor($"{message} - {ex}", ConsoleColor.Yellow);
}

public void Error(string message)
{
WriteLineInColor($"{message}", ConsoleColor.Red);
}

public void Error(Exception ex, string message)
{
WriteLineInColor($"{message} - {ex}", ConsoleColor.Red);
}

public void Fatal(string message)
{
WriteLineInColor($"{message}", ConsoleColor.DarkRed);
}

public void Fatal(Exception ex, string message)
{
WriteLineInColor($"{message} - {ex}", ConsoleColor.DarkRed);
}
}
}
38 changes: 36 additions & 2 deletions FluentlySharepoint/FluentlySharePoint/Models/TaxonomyOperation.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Taxonomy;

Expand All @@ -12,6 +13,16 @@ public enum LockLevel
}
public class TaxonomyOperation
{
private readonly CSOMOperation operation;

public TaxonomyOperation(CSOMOperation operation)
{
this.operation = operation;
}

[Obsolete("Not used yet")]
public Queue<DeferredAction> ActionQueue { get; } = new Queue<DeferredAction>(10);

public Dictionary<string, TermGroup> LoadedTermGroups { get; } = new Dictionary<string, TermGroup>(5);
public Dictionary<string, TermSet> LoadedTermSets { get; } = new Dictionary<string, TermSet>(5);
public Dictionary<string, Term> LoadedTerms { get; } = new Dictionary<string, Term>(5);
Expand All @@ -23,6 +34,29 @@ public class TaxonomyOperation
public TermGroup LastTermGroup { get; set; }
public TermSet LastTermSet { get; set; }
public Term LastTerm { get; set; }



[Obsolete("Not used yet")]
private void ProcessLoaded(ClientObject clientObject)
{
switch (clientObject)
{
case TermGroup g:
LoadedTermGroups.AddOrUpdate(g.Name.ToLower(), g);
break;
case TermSet s:
LoadedTermSets.AddOrUpdate(s.Name.ToLower(), s);
break;
//case List t:
// LoadedLists[l.Title] = l;
// break;
//case WebCollection wc:
// wc.ToList().ForEach(ProcessLoaded);
// break;
//case ListCollection lc:
// lc.ToList().ForEach(ProcessLoaded);
// break;
}
}
}
}
63 changes: 0 additions & 63 deletions FluentlySharepoint/TestConsole/Loggers/ConsoleLogger.cs

This file was deleted.

Loading

0 comments on commit 712b048

Please sign in to comment.