Advertisement
Guest User

Untitled

a guest
May 26th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.49 KB | None | 0 0
  1. using Microsoft.SharePoint;
  2. using Microsoft.SharePoint.Taxonomy;
  3.  
  4. namespace CallTaxonomyWebService
  5. {
  6. /// <summary>
  7. /// A class used for retrieving MMS data. This must be run on the server
  8. /// which is hosting the metadata.
  9. /// </summary>
  10. class ServerSideMMS
  11. {
  12. private const string ManagedMetadataService = "Managed Metadata Service";
  13. private TaxonomySession Taxonomy;
  14. private SPSite Site;
  15.  
  16. /// <summary>
  17. /// The constructor assigns the given SPSite and the resulting TaxonomySession
  18. /// to some private fields.
  19. /// </summary>
  20. /// <param name="spSite"></param>
  21. public ServerSideMMS(string spSite)
  22. {
  23. Site = new SPSite(spSite);
  24. Taxonomy = new TaxonomySession(Site);
  25. }
  26.  
  27. /// <summary>
  28. /// Get all the terms which are stored in the given termset under the given group
  29. /// </summary>
  30. /// <param name="groupName"></param>
  31. /// <param name="setName"></param>
  32. /// <returns></returns>
  33. public TermCollection GetTermSet(string groupName, string setName)
  34. {
  35. var group = GetGroup(groupName);
  36. var termset = group.TermSets[setName];
  37. return termset.Terms;
  38. }
  39.  
  40. /// <summary>
  41. /// Returns an MMS group of the given name
  42. /// </summary>
  43. /// <param name="groupName"></param>
  44. /// <returns></returns>
  45. public Group GetGroup(string groupName)
  46. {
  47. var Mms = Taxonomy.TermStores[ManagedMetadataService];
  48. return Mms.Groups[groupName];
  49. }
  50. }
  51. }
  52.  
  53. using Microsoft.SharePoint.Client;
  54. using Microsoft.SharePoint.Client.Taxonomy;
  55.  
  56. namespace CallTaxonomyWebService
  57. {
  58. /// <summary>
  59. /// A class used for retrieving MMS data. This can be run from a client machine.
  60. /// Because of the more complicated nature of these queryies (when compared to
  61. /// the server-side calls), I have defined a few private fields to store
  62. /// return data.
  63. ///
  64. /// For more on what we can do with CSOM and MMS, see https://github.com/OfficeDev/PnP/tree/dev and
  65. /// https://msdn.microsoft.com/en-us/library/office/dn904534.aspx.
  66. /// </summary>
  67. class ClientSideMMS
  68. {
  69. private TaxonomySession Taxonomy;
  70. private ClientContext Context;
  71. private TermStore Store;
  72. private TermGroup Group;
  73. private TermCollection Collection;
  74.  
  75. /// <summary>
  76. /// The constructor sets and validates the ClientContext before loading
  77. /// the required elements of each Store, Group, TermSet and Term. This can
  78. /// be amended if we every want to retrieve more data but for now I am
  79. /// just getting the names of each.
  80. /// </summary>
  81. /// <param name="spSite"></param>
  82. public ClientSideMMS(string spSite)
  83. {
  84. Context = new ClientContext(spSite)
  85. { AuthenticationMode = ClientAuthenticationMode.Default};
  86. Taxonomy = TaxonomySession.GetTaxonomySession(Context);
  87. Store = Taxonomy.GetDefaultSiteCollectionTermStore();
  88. Context.Load(Store,
  89. store => store.Name,
  90. store => store.Groups.Include(
  91. group => group.Name,
  92. group => group.TermSets.Include(
  93. termSet => termSet.Name,
  94. termSet => termSet.Terms.Include(
  95. term => term.Name)
  96. )
  97. )
  98. );
  99. Context.ExecuteQuery();
  100. }
  101.  
  102. /// <summary>
  103. /// This returns the private "Set" field after calling "LoadTermSet" (which assigns the
  104. /// requested TermSet to Set)
  105. /// </summary>
  106. /// <param name="groupName"></param>
  107. /// <param name="setName"></param>
  108. /// <returns></returns>
  109. public TermCollection GetTermSet(string groupName, string setName)
  110. {
  111. LoadTermSet(groupName, setName);
  112. return Collection;
  113. }
  114.  
  115. /// <summary>
  116. /// Iterate through each Group until we find the right one (Name = groupName). Then
  117. /// find the correct TermSet in that group and assign the Set field to that value.
  118. ///
  119. /// This is structured like this to avoid local "return" variables ...
  120. /// </summary>
  121. /// <param name="groupName"></param>
  122. /// <param name="setName"></param>
  123. private void LoadTermSet(string groupName, string setName)
  124. {
  125. foreach (var group in Store.Groups)
  126. {
  127. if (group.Name == groupName)
  128. {
  129. Group = group;
  130.  
  131. foreach (var set in group.TermSets)
  132. {
  133. if (set.Name == setName)
  134. {
  135. // I would much rather this function just return the Collection,
  136. // but as I can't guaruntee that a collection will be found, this
  137. // could cause an error, hence the private field ...
  138. Collection = set.Terms;
  139. return;
  140. }
  141. }
  142. }
  143. }
  144. }
  145. }
  146. }
  147.  
  148. using System.Linq;
  149.  
  150. namespace CallTaxonomyWebService
  151. {
  152. static class Program
  153. {
  154. private const string site = @"http://mysharepointsite/";
  155.  
  156. private static void Main(string[] args)
  157. {
  158. var mms = new ClientSideMMS(site);
  159. var termcollection = mms.GetTermSet("People", "Department");
  160. var localTerms = termcollection.Select(term => term.Name).ToList();
  161.  
  162. var remoteMms = new ServerSideMMS(site);
  163. var remoteCollection = remoteMms.GetTermSet("People", "Department");
  164. var remoteTerms = remoteCollection.Select(term => term.Name).ToList();
  165. }
  166. }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement