This repository was archived by the owner on Feb 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSPContentType.cs
More file actions
150 lines (134 loc) · 7.03 KB
/
SPContentType.cs
File metadata and controls
150 lines (134 loc) · 7.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* Copyright (C) 2014 NAVERTICA a.s. http://www.navertica.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.SharePoint;
namespace Navertica.SharePoint.Extensions
{
public static class SPContentTypeExtensions
{
/// <summary>
/// Checks if content type collection contains content type with specified contentTypeId or name as string.
/// </summary>
/// <param name="contentTypeCollection"></param>
/// <param name="identification">will be trimmed</param>
/// <returns></returns>
public static bool Contains(this SPContentTypeCollection contentTypeCollection, string identification)
{
if (contentTypeCollection == null) throw new ArgumentNullException("contentTypeCollection");
if (identification == null) throw new ArgumentNullException("identification");
if (identification.Trim() == string.Empty) throw new ArgumentException("empty identification");
try
{
// in case identification is ContentTypeId
return Contains(contentTypeCollection, new SPContentTypeId(identification.Trim()));
}
catch (ArgumentException) {} // when identification doesn't look like SPContentTypeId
// in case identification is name
List<string> list = contentTypeCollection.Cast<SPContentType>().Select(ct => ct.Name).ToList();
return list.Any(name => name == identification.Trim());
}
/// <summary>
/// Checks if content type collection contains content with given contentTypeId
/// </summary>
/// <param name="contentTypeCollection"></param>
/// <param name="contentTypeId"></param>
/// <returns></returns>
public static bool Contains(this SPContentTypeCollection contentTypeCollection, SPContentTypeId contentTypeId)
{
if (contentTypeCollection == null) throw new ArgumentNullException("contentTypeCollection");
if (contentTypeId == null) throw new ArgumentNullException("contentTypeId");
List<string> list = contentTypeCollection.Cast<SPContentType>().Select(ct => ct.Id.ToString()).ToList();
return list.Any(ctId => ctId.StartsWith(contentTypeId.ToString())); // CHECK - does adding 0x0108 to a list mean the list will accept 0x01 also?
}
/// <summary>
/// Checks whether the list contains all the fields with internal names passed in intFieldNames
/// </summary>
/// <param name="contentType"></param>
/// <param name="intFieldNames">internal names the list should contain</param>
/// <returns></returns>
public static bool ContainsFieldIntName(this SPContentType contentType, IEnumerable<string> intFieldNames)
{
if (contentType == null) throw new ArgumentNullException("contentType");
if (intFieldNames == null) throw new ArgumentNullException("intFieldNames");
foreach (string fieldname in intFieldNames)
{
try
{
contentType.Fields.GetFieldByInternalName(fieldname.Trim());
}
catch
{
return false;
}
}
return true;
}
/// <summary>
/// Checks whether the content type contains a field with internal name
/// </summary>
/// <param name="contentType"></param>
/// <param name="fieldName">field internal name - will be trimmed</param>
/// <returns></returns>
public static bool ContainsFieldIntName(this SPContentType contentType, string fieldName)
{
if (contentType == null) throw new ArgumentNullException("contentType");
if (fieldName == null) throw new ArgumentNullException("fieldName");
if (fieldName.Trim() == string.Empty) throw new ArgumentException("fieldName is empty string");
try
{
contentType.Fields.GetFieldByInternalName(fieldName);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// Get content type with specified contentTypeId
/// </summary>
/// <param name="contentTypeCollection"></param>
/// <param name="contentTypeId">content type id as string or name - will be trimmed</param>
/// <returns></returns>
public static SPContentType GetContentType(this SPContentTypeCollection contentTypeCollection, SPContentTypeId contentTypeId)
{
if (contentTypeCollection == null) throw new ArgumentNullException("contentTypeCollection");
if (contentTypeId == null) throw new ArgumentNullException("contentTypeId");
return GetContentType(contentTypeCollection, contentTypeId.ToString());
}
/// <summary>
/// Get content type with specified contentTypeId or name from collection
/// </summary>
/// <param name="contentTypeCollection"></param>
/// <param name="identification">content type id as string or name - will be trimmed</param>
/// <returns></returns>
public static SPContentType GetContentType(this SPContentTypeCollection contentTypeCollection, string identification)
{
if (contentTypeCollection == null) throw new ArgumentNullException("contentTypeCollection");
if (identification == null) throw new ArgumentNullException("identification");
if (identification.Trim() == string.Empty) throw new ArgumentException("identification is empty string");
try
{
SPContentTypeId searchCtId = new SPContentTypeId(identification);
//if the contentTypeCollection is from SPList we are looking for their parent, TODO mozna dat dalsi parametr bool at cekuje i rodice to samy k funckcim contains
return contentTypeCollection.Cast<SPContentType>().Single(ct => ct.Id == searchCtId || ct.Parent.Id == searchCtId); //identification is ContentTypeId
}
catch
{
return contentTypeCollection.Cast<SPContentType>().SingleOrDefault(ct => ct.Name == identification.Trim()); //identification is ContentType name
}
}
}
}