-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreplace_webpart.js
232 lines (188 loc) · 12.5 KB
/
replace_webpart.js
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
var wpXml = '<?xml version=\"1.0\" encoding=\"utf-8\"?>'
+ ' <webParts> '
+ ' <webPart xmlns="http://schemas.microsoft.com/WebPart/v3"> '
+ ' <metaData> '
+ ' <type name="Microsoft.Office.Server.Search.WebControls.ContentBySearchWebPart, Microsoft.Office.Server.Search, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" /> '
+ ' <importErrorMessage>Cannot import this Web Part.</importErrorMessage> '
+ ' </metaData> '
+ ' <data> '
+ ' <properties> '
+ ' <property name="StatesJson" type="string">{}</property> '
+ ' <property name="UseSharedDataProvider" type="bool">False</property> '
+ ' <property name="UseSimplifiedQueryBuilder" type="bool">False</property> '
+ ' <property name="QueryGroupName" type="string">9319aa92-ac21-43e9-bd90-3603685cbff5</property> '
+ ' <property name="LogAnalyticsViewEvent" type="bool">False</property> '
+ ' <property name="SelectedPropertiesJson" type="string">["Path","LastModifiedTime","Title","CommunicationDescriptionOWSMTXT","SecondaryFileExtension","ContentTypeId"]</property> '
+ ' <property name="PropertyMappings" type="string">Link URL{Link URL}:Path,Line 1{Line 1}:LastModifiedTime,Line 2{Line 2}:Title,Line 3{Line 3}:CommunicationDescriptionOWSMTXT,SecondaryFileExtension,ContentTypeId</property> '
+ ' <property name="ShowAdvancedLink" type="bool">True</property> '
+ ' <property name="NumberOfItems" type="int">2</property> '
+ ' <property name="EmitStyleReference" type="bool">True</property> '
+ ' <property name="ShowPreferencesLink" type="bool">True</property> '
+ ' <property name="ServerIncludeScriptsJson" type="string">null</property> '
+ ' <property name="IncludeResultTypeConstraint" type="bool">False</property> '
+ ' <property name="Height" type="string" /> '
+ ' <property name="MaxPagesBeforeCurrent" type="int">4</property> '
+ ' <property name="ResultType" type="string" /> '
+ ' <property name="ShowDidYouMean" type="bool">False</property> '
+ ' <property name="StartingItemIndex" type="int">1</property> '
+ ' <property name="AlwaysRenderOnServer" type="bool">False</property> '
+ ' <property name="GroupTemplateId" type="string">~sitecollection/_catalogs/masterpage/Display Templates/Content Web Parts/Group_Content.js</property> '
+ ' <property name="ResultTypeId" type="string" /> '
+ ' <property name="ItemTemplateId" type="string">~sitecollection/_catalogs/masterpage/Display Templates/Content Web Parts/Item_PCHEMNews.js</property> '
+ ' <property name="AllowConnect" type="bool">True</property> '
+ ' <property name="HelpUrl" type="string" /> '
+ ' <property name="ResultsPerPage" type="int">2</property> '
+ ' <property name="RenderTemplateId" type="string">~sitecollection/_catalogs/masterpage/Display Templates/Content Web Parts/Control_List.js</property> '
+ ' <property name="AllowEdit" type="bool">True</property> '
+ ' <property name="AllowZoneChange" type="bool">True</property> '
+ ' <property name="AddSEOPropertiesFromSearch" type="bool">False</property> '
+ ' <property name="AdvancedSearchPageAddress" type="string">advanced.aspx</property> '
+ ' <property name="HitHighlightedPropertiesJson" type="string">["Title","Path","Author","SectionNames","SiteDescription"]</property> '
+ ' <property name="TitleUrl" type="string" /> '
+ ' <property name="EmptyMessage" type="string" /> '
+ ' <property name="ShowBestBets" type="bool">False</property> '
+ ' <property name="AllowHide" type="bool">True</property> '
+ ' <property name="BypassResultTypes" type="bool">True</property> '
+ ' <property name="Description" type="string">Customized Content Search Webpart with Date, Title w Link and Description</property> '
+ ' <property name="ShowSortOptions" type="bool">False</property> '
+ ' <property name="ExportMode" type="exportmode">All</property> '
+ ' <property name="AllowMinimize" type="bool">True</property> '
+ ' <property name="ShowPersonalFavorites" type="bool">False</property> '
+ ' <property name="ChromeType" type="chrometype">TitleOnly</property> '
+ ' <property name="ShowPaging" type="bool">True</property> '
+ ' <property name="ChromeState" type="chromestate">Normal</property> '
+ ' <property name="CatalogIconImageUrl" type="string" /> '
+ ' <property name="HelpMode" type="helpmode">Modeless</property> '
+ ' <property name="TitleIconImageUrl" type="string" /> '
+ ' <property name="ItemBodyTemplateId" type="string" /> '
+ ' <property name="AlternateErrorMessage" type="string" null="true" /> '
+ ' <property name="Hidden" type="bool">False</property> '
+ ' <property name="TargetResultTable" type="string">RelevantResults</property> '
+ ' <property name="AllowClose" type="bool">True</property> '
+ ' <property name="MissingAssembly" type="string">Cannot import this Web Part.</property> '
+ ' <property name="ShowResultCount" type="bool">True</property> '
+ ' <property name="ShowLanguageOptions" type="bool">True</property> '
+ ' <property name="ShowUpScopeMessage" type="bool">False</property> '
+ ' <property name="Width" type="string" /> '
+ ' <property name="RepositionLanguageDropDown" type="bool">False</property> '
+ ' <property name="Title" type="string">Key Communications</property> '
+ ' <property name="ScrollToTopOnRedraw" type="bool">False</property> '
+ ' <property name="ShowResults" type="bool">True</property> '
+ ' <property name="ShowAlertMe" type="bool">True</property> '
+ ' <property name="OverwriteResultPath" type="bool">True</property> '
+ ' <property name="PreloadedItemTemplateIdsJson" type="string">null</property> '
+ ' <property name="MaxPagesAfterCurrent" type="int">1</property> '
+ ' <property name="ShowDefinitions" type="bool">False</property> '
+ ' <property name="ShouldHideControlWhenEmpty" type="bool">True</property> '
+ ' <property name="ShowViewDuplicates" type="bool">False</property> '
+ ' <property name="AvailableSortsJson" type="string">null</property> '
+ ' <property name="DataProviderJSON" type="string">{"QueryGroupName":"9319aa92-ac21-43e9-bd90-3603685cbff5","QueryPropertiesTemplateUrl":"sitesearch://webroot","IgnoreQueryPropertiesTemplateUrl":false,"SourceID":"8413cd39-2156-4e00-b54d-11efd9abdb89","SourceName":"Local SharePoint Results","SourceLevel":"Ssa","CollapseSpecification":"","QueryTemplate":"path:{Site.Url} ContentTypeId:\\"0x0100DA897DFE18ABF642A9D56CA3F0075F82*\\" FeaturedOWSBOOL=1\\n(Date00\\u003e{Today} OR (-ExpiryDateOWSDATE:\\u00270*\\u0027 AND -ExpiryDateOWSDATE:\\u00271*\\u0027 AND -ExpiryDateOWSDATE:\\u00272*\\u0027 AND -ExpiryDateOWSDATE:\\u00273*\\u0027 AND -ExpiryDateOWSDATE:\\u00274*\\u0027 AND -ExpiryDateOWSDATE:\\u00275*\\u0027 AND -ExpiryDateOWSDATE:\\u00276*\\u0027 AND -ExpiryDateOWSDATE:\\u00277*\\u0027 AND -ExpiryDateOWSDATE:\\u00278*\\u0027 AND -ExpiryDateOWSDATE:\\u00279*\\u0027 ))","FallbackSort":[{"p":"LastModifiedTime","d":1}],"FallbackSortJson":"[{\\"p\\":\\"LastModifiedTime\\",\\"d\\":1}]","RankRules":null,"RankRulesJson":"null","AsynchronousResultRetrieval":false,"SendContentBeforeQuery":true,"BatchClientQuery":true,"FallbackLanguage":-1,"FallbackRankingModelID":"","EnableStemming":true,"EnablePhonetic":false,"EnableNicknames":false,"EnableInterleaving":false,"EnableQueryRules":true,"EnableOrderingHitHighlightedProperty":false,"HitHighlightedMultivaluePropertyLimit":-1,"IgnoreContextualScope":true,"ScopeResultsToCurrentSite":false,"TrimDuplicates":false,"Properties":{"TryCache":true,"Scope":"{Site.URL}","UpdateLinksForCatalogItems":true,"EnableStacking":true,"ListId":"2afbf720-186e-470d-9654-35d64511df81","ListItemId":1},"PropertiesJson":"{\\"TryCache\\":true,\\"Scope\\":\\"{Site.URL}\\",\\"UpdateLinksForCatalogItems\\":true,\\"EnableStacking\\":true,\\"ListId\\":\\"2afbf720-186e-470d-9654-35d64511df81\\",\\"ListItemId\\":1}","ClientType":"ContentSearchRegular","UpdateAjaxNavigate":true,"SummaryLength":180,"DesiredSnippetLength":90,"PersonalizedQuery":false,"FallbackRefinementFilters":null,"IgnoreStaleServerQuery":false,"RenderTemplateId":"DefaultDataProvider","AlternateErrorMessage":null,"Title":""}</property> '
+ ' <property name="Direction" type="direction">NotSet</property> '
+ ' </properties> '
+ ' </data> '
+ ' </webPart> '
+ ' </webParts> '
;
var deleteKeyCommunciationsWebPart = function(ctx, limitedWebPartManager) {
var deferred = jQuery.Deferred();
jQuery.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetFileByServerRelativeUrl('" + ctx.get_url() + "/pages/default.aspx" + "')/GetLimitedWebPartManager(0)/WebParts?$expand=WebPart",
type: "get",
headers: {
"accept": "application/json;odata=verbose",
"content-Type": "application/json;odata=verbose"
},
success: function(data) {
console.log(data);
console.log(data.d.results);
window.results = data.d.results;
var index = -1;
jQuery.each(data.d.results, function(i,v) {
if(v.WebPart.Title == webPartTitle){
index = i;
return false;
}
});
if(index >= 0){
var webPartsClient = limitedWebPartManager.get_webParts();
//var webParts
ctx.load(webPartsClient);
ctx.executeQueryAsync(function() {
webPartsClient.get_item(index).deleteWebPart();
ctx.executeQueryAsync(function() {
console.log("deleted "+ webPartTitle + " web part");
deferred.resolve();
});
});
}
else {
console.log("Did not find " + webPartTitle + " web part");
deferred.resolve();
}
},
error: function(error) {
console.log(JSON.stringify(error));
}
});
return deferred.promise();
}
var replaceKeyCommunicationsWebPart = function(clientContext){
console.log("Loading Data..");
var website = clientContext.get_web();
var serverRelativeUrl = clientContext.get_url() + "/pages/default.aspx";
console.log("serverrelativeurl: " + serverRelativeUrl);
var file = website.getFileByServerRelativeUrl(serverRelativeUrl);
clientContext.load(file);
clientContext.executeQueryAsync(function() {
console.log("Loaded File: " + file.get_title());
file.checkOut();
clientContext.executeQueryAsync(function() {
console.log("Loaded File: " + file.get_title());
var limitedWebPartManager = file.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
deleteKeyCommunciationsWebPart(clientContext, limitedWebPartManager).done(function() {
console.log("Adding WP to page");
var wp = limitedWebPartManager.importWebPart(wpXml);
limitedWebPartManager.addWebPart(wp.get_webPart(), "LeftColumn", webPartIndex);
file.checkIn();
file.publish("Published via script");
clientContext.executeQueryAsync(function() {
console.log("Checked in & published file: " + file.get_title());
}, function() {console.log("fail check in")});
});
}, function() {console.log("fail")});
}, function(sender, args) {console.log("fail: "); console.log(sender); console.log(args);});
}
var onload = function() {
var competencyUrls = [
//Competencies
"/teams/SHEEC_PORTAL1/auditing/",
//"/teams/SHEEC_PORTAL1/data-systems-reporting",
"/teams/SHEEC_PORTAL1/distribution",
"/teams/SHEEC_PORTAL1/emergency-responses",
"/teams/SHEEC_PORTAL1/environmental-stewardship",
"/teams/SHEEC_PORTAL1/ergonomics",
"/teams/SHEEC_PORTAL1/fire-safety",
"/teams/SHEEC_PORTAL1/occupational-health",
"/teams/SHEEC_PORTAL1/occupational-medicine",
"/teams/SHEEC_PORTAL1/psm",
"/teams/SHEEC_PORTAL1/workplace-safety"
//Businesses
// "/teams/SHEEC_PORTAL1/EHSChemSol",
// "/teams/SHEEC_PORTAL1/EHSFluoro",
// "/teams/SHEEC_PORTAL1/EHSTTech"
]
for(var i = 0; i < competencyUrls.length; i++) {
var clientContext = new SP.ClientContext(competencyUrls[i]);
replaceKeyCommunicationsWebPart(clientContext);
}
};
/* REMEMBER BOTH OF THESE */
var webPartTitle = "Key Communications";
var webPartIndex = 0;
/**************************/
//
// SP.SOD.registerSod("jquery", "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js");
// SP.SOD.executeFunc("jquery", null, onload);
// SP.SOD.executeOrDelayUntilScriptLoaded(onload, "sp.js");
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', onload);
//SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function() {console.log("hello")});