Saturday, February 24, 2018

How to use SyntaxHighlighter in Google Blogspot blogger engine: manual installation

For writing my previous posts I used Windows Live Write app. It has good enough syntax highlight plugin – it was not perfect but was good enough for my purposes – you may check example e.g. in the following post: One way to avoid “Term update failed because of save conflict” error when create managed metadata terms in Sharepoint. As you probably know MS discontinued Windows Live Writer some time ago and now there is it’s open source version called Open Live Write (which I use now also for writing this post). Unfortunately previous syntax highlight plugin doesn’t work and I needed to search alternative.

One of the first plugins which Google showed me was Windows/Open Live Writer Code Plugin. But as you may see on this page it says

The Live Writer Source Code Plug-in For WordPress.com Blogs.

When I tried to use it with my Blogspot blog it didn’t work.

There is popular source code highlighter library SyntaxHighlighter from Alex Gorbatchev. Unfortunately currently there is no available plugin for Open Live Writer which would utilize SyntaxHighlighter, but I found a way how to install it manually and want to share it with you. Of course this approach is not so convenient as using plugin but it is better than nothing.

First of all I would like to mention that at the moment of writing this post official project build script on GitHub doesn’t work because of the following issue: Building: loadReposFromCache(...).error is not a function. So I will describe manual installation.

1. First of all you need to encode your source code to replace < and > by &lt; and &gt; e.g. using this page: https://opinionatedgeek.com/Codecs/HtmlEncoder.

2. Then put your source code within <pre class="brush: {brush_name}"></pre> tags (you need to use Source table of Open Live Writer for that). Here {brush_name} should be replaced with brush name for used programming language. List of supported brushes can be found here: https://github.com/syntaxhighlighter/syntaxhighlighter/wiki/Brushes-and-Themes. Click on your programming language title and found brush.js file – if you will scroll it you will find exact brush aliases which can be used in class attribute. E.g. for C#:

Brush.aliases = ['c#', 'c-sharp', 'csharp'];

So for C# and XML tags will be the following:

<pre class="brush: csharp"></pre>
<pre class="brush: xml"></pre>

3. Add the following css and script references to the source tab of your post on top:

<link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css' rel='stylesheet' type='text/css'>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shAutoloader.js' type='text/javascript'></script>

These 4 references are common – they will be needed for all your posts. After them add references to needed brushes (which programming languages will be used in your post. List of exact file names can be found here: https://cdnjs.com/libraries/SyntaxHighlighter). E.g. if we use XML, javascript, css and C# we need to add the following references:
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCss.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCSharp.js' type='text/javascript'></script>

If you don’t want to use CDN from http://alexgorbatchev.com you may copy these files to your Google Drive and use them like described in the following article: http://www.bloggerseolab.com/2017/02/host-images-javascript-and-css-on-google-drive.html.

4. After above references add javascript call which will make exact highlight:

<script type="text/javascript">
  document.addEventListener("DOMContentLoaded", function(event) { 
    SyntaxHighlighter.highlight();
  });
</script>

After that publish your post and enjoy of syntax highlighting). In this post I used same method for highlighting last code sample so you may see how it works.

Friday, February 23, 2018

Camlex 4.3 and Camlex.Client 2.3 for Sharepoint are released

I’m glad to announce that today next versions of Camlex open source library have been releases:

  • Camlex 4.3 – for basic Sharepoint server object model
  • Camlex.Client 2.3 – for client-side object model (CSOM)

Camlex is a free library which simplifies creation of CAML queries (both static CAML queries when number of conditions is known at compile time and dynamic CAML queries when number of conditions will be known only in runtime).

In new version support for native float and decimal types was added. I.e. now you may use these types in queries explicitly without need to cast to double which was the only floating type supported before today’s release. And similar to double float and decimal values will be translated to Number type:

string caml = Camlex.Query().Where(x => (float)x["Rate"] == 1.5f).ToString();

will produce:

<Where>
  <Eq>
    <FieldRef Name="Rate" />
    <Value Type="Number">1.5</Value>
  </Eq>
</Where>

And with decimal:

string caml = Camlex.Query().Where(x => (decimal)x["Rate"] == 1.5m).ToString();

output result will be the same.

New packages are also updated in Nuget: they have Validating status now and will be available after some time when Nuget will complete antivirus checks.

And I’m especially glad today because this is the first release since Camlex got new home at GitHub https://github.com/sadomovalex/camlex (it was moved there last year from Codeplex since last one is discontinued now). Development of the library is on going and I will continue to do it more valuable for Sharepoint community in future as well.

Thursday, February 15, 2018

Change UI language for Windows 10 single language edition

Recently I needed to change language for Windows 10 single language edition. When PC was purchased it had non-English language installed and as it was single language edition (you may check it in My PC > Properties) it was not possible to change language from OS UI on English (for me it is simpler to have it on English). However at the end it became possible to change it from command line (see below). The most tricky part is to get correct version of needed language pack. In my case OS had 10.0.15063.0 build and correct language packs links for this build are listed here: Language Pack (Build 15063 – 1703). In case link won’t work here are the links:

Windows 10 build 15063 64-bit MUI/LPs

af-ZADownload link
am-ETDownload link
ar-SADownload link
as-INDownload link
az-Latn-AZDownload link
be-BYDownload link
bg-BGDownload link
bn-BDDownload link
bn-INDownload link
bs-Latn-BADownload link
ca-ESDownload link
ca-ES-valenciaDownload link
chr-CHER-USDownload link
cs-CZDownload link
cy-GBDownload link
da-DKDownload link
de-DEDownload link
el-GRDownload link
en-GBDownload link
en-USDownload link
es-ESDownload link
es-MXDownload link
et-EEDownload link
eu-ESDownload link
fa-IRDownload link
fi-FIDownload link
fil-PHDownload link
fr-CADownload link
fr-FRDownload link
ga-IEDownload link
gd-GBDownload link
gl-ESDownload link
gu-INDownload link
ha-Latn-NGDownload link
he-ILDownload link
hi-INDownload link
hr-HRDownload link
hu-HUDownload link
hy-AMDownload link
id-IDDownload link
ig-NGDownload link
is-ISDownload link
it-ITDownload link
ja-JPDownload link
ka-GEDownload link
kk-KZDownload link
km-KHDownload link
kn-INDownload link
kok-INDownload link
ko-KRDownload link
ku-Arab-IQDownload link
ky-KGDownload link
lb-LUDownload link
lo-LADownload link
lt-LTDownload link
lv-LVDownload link
mi-NZDownload link
mk-MKDownload link
ml-INDownload link
mn-MNDownload link
mr-INDownload link
ms-MYDownload link
mt-MTDownload link
nb-NODownload link
ne-NPDownload link
nl-NLDownload link
nn-NODownload link
nso-ZADownload link
or-INDownload link
pa-Arab-PKDownload link
pa-INDownload link
pl-PLDownload link
prs-AFDownload link
pt-BRDownload link
pt-PTDownload link
quc-Latn-GTDownload link
quz-PEDownload link
ro-RODownload link
ru-RUDownload link
rw-RWDownload link
sd-ArDownload link
si-LKDownload link
sk-SKDownload link
sl-SIDownload link
sq-ALDownload link
sr-Cyrl-BADownload link
sr-Cyrl-RSDownload link
sr-Latn-RSDownload link
sv-SEDownload link
sw-KEDownload link
ta-INDownload link
te-INDownload link
tg-Cyrl-TJDownload link
th-THDownload link
ti-ETDownload link
tk-TMDownload link
tn-ZADownload link
tr-TRDownload link
tt-RUDownload link
ug-CNDownload link
uk-UADownload link
ur-PKDownload link
uz-Latn-UZDownload link
vi-VNDownload link
wo-SNDownload link
xh-ZADownload link
yo-NGDownload link
zh-CNDownload link
zh-TWDownload link
zu-ZADownload link

Windows 10 build 15063 32-bit MUI/LPs

af-ZADownload link
am-ETDownload link
ar-SADownload link
as-INDownload link
az-Latn-AZDownload link
be-BYDownload link
bg-BGDownload link
bn-BDDownload link
bn-INDownload link
bs-Latn-BADownload link
ca-ESDownload link
ca-ES-valenciaDownload link
chr-CHER-USDownload link
cs-CZDownload link
cy-GBDownload link
da-DKDownload link
de-DEDownload link
el-GRDownload link
en-GBDownload link
en-USDownload link
es-ESDownload link
es-MXDownload link
et-EEDownload link
eu-ESDownload link
fa-IRDownload link
fi-FIDownload link
fil-PHDownload link
fr-CADownload link
fr-FRDownload link
ga-IEDownload link
gd-GBDownload link
gl-ESDownload link
gu-INDownload link
ha-Latn-NGDownload link
he-ILDownload link
hi-INDownload link
hr-HRDownload link
hu-HUDownload link
hy-AMDownload link
id-IDDownload link
ig-NGDownload link
is-ISDownload link
it-ITDownload link
ja-JPDownload link
ka-GEDownload link
kk-KZDownload link
km-KHDownload link
kn-INDownload link
kok-INDownload link
ko-KRDownload link
ku-Arab-IQDownload link
ky-KGDownload link
lb-LUDownload link
lo-LADownload link
lt-LTDownload link
lv-LVDownload link
mi-NZDownload link
mk-MKDownload link
ml-INDownload link
mn-MNDownload link
mr-INDownload link
ms-MYDownload link
mt-MTDownload link
nb-NODownload link
ne-NPDownload link
nl-NLDownload link
nn-NODownload link
nso-ZADownload link
or-INDownload link
pa-Arab-PKDownload link
pa-INDownload link
pl-PLDownload link
prs-AFDownload link
pt-BRDownload link
pt-PTDownload link
quc-Latn-GTDownload link
quz-PEDownload link
ro-RODownload link
ru-RUDownload link
rw-RWDownload link
sd-ArDownload link
si-LKDownload link
sk-SKDownload link
sl-SIDownload link
sq-ALDownload link
sr-Cyrl-BADownload link
sr-Cyrl-RSDownload link
sr-Latn-RSDownload link
sv-SEDownload link
sw-KEDownload link
ta-INDownload link
te-INDownload link
tg-Cyrl-TJDownload link
th-THDownload link
ti-ETDownload link
tk-TMDownload link
tn-ZADownload link
tr-TRDownload link
tt-RUDownload link
ug-CNDownload link
uk-UADownload link
ur-PKDownload link
uz-Latn-UZDownload link
vi-VNDownload link
wo-SNDownload link
xh-ZADownload link
yo-NGDownload link
zh-CNDownload link
zh-TWDownload link
zu-ZADownload link

After you get correct language pack run the following commands in cmd as administrator:

1. Install new language pack:

dism /Online /Add-Package /PackagePath:C:\lp.cab

(assume that language pack is copied to c:\ drive and has name lp.cab)

2. List installed language packs:

dism /Online /Get-Packages

3. Copy identifier of previous language pack which came with the system and remove it:

dism /Online /Remove-Package /PackageName:{package_id}

Where instead of {package_id} you should use copied language pack identifier. After that restart Windows (it will ask you to do that) and after some time when it will be restarted OS UI will use new language.

Monday, January 29, 2018

Internal details of setting Access request settings for Sharepoint sites

In Sharepoint you may configure Access request settings which will allow users to ask for access to the site from appropriate responsible person (I intentially don’t tell site owner here because recipient of access requests may be different person – see below). In order to do it first of all you need to configure outgoing email settings in Central administration. Access request settings for web site may be configured from Site settings > Site permissions > Access request settings:

When you click this link Sharepoint loads setrqacc.aspx into modal window. If we will check its codebehind class Microsoft.SharePoint.ApplicationPages.SetRequestAccess we will see that it first checks SPWebApplication.RequestAccessEnabled internal property:

In this property it checks another property IsEmailServerSet:

which in turn checks outgoing email server address:

That’s why setting outgoing email settings should be done before to configure Access request settings.

Now let’s check SetRequestAccess.OnLoad method again and see how “Allow access requests” checkbox is initialized with checked/unckecked values. As it is shown above OnLoad method calls ToggleSelectedAndSetObjectType method:

and the last one checks “Allow access requests” checkbox when SPWeb.RequestAccessEnabled property set to true. Let’s check code of SPWeb.RequestAccessEnabled property:

I.e. it is readonly property which returns true only when Access request email is set, i.e. other property SPWeb.RequestAccessEmail. So basically in order to enable Access requests you need to set SPWeb.RequestAccessEmail for your web site.

The problem however is that Sharepoint for some sites sets SPWeb.RequestAccessEmail to default value someone@example.com – see first picture in this post. There is internal method SPWeb.EnableAccessRequestsIfNeeded where it is done:

Here it’s code as it doesn’t fit to post column’s width:

   1:  
   2: private void EnableAccessRequestsIfNeeded()
   3: {
   4:     if (string.IsNullOrWhiteSpace(this.RequestAccessEmail) &&
   5:         this.HasUniqueRoleAssignments && this.WebTemplateId != 3
   6:         && this.WebTemplateId != 16 && this.WebTemplateId != 17 &&
   7:         this.WebTemplateId != 18 && this.Site.WebApplication.RequestAccessEnabled)
   8:     {
   9:         this.RequestAccessEmail = "someone@example.com";
  10:     }
  11: }

So for root webs of new site collections which have HasUniqueRoleAssignments = true and which use web template id except 3, 16, 17, 18 (and of course if SPWebApplication.RequestAccessEnabled is set to true – see above) Sharepoint will set SPWeb.RequestAccessEmail to default value someone@example.com which will mean that Access requests will be enabled there by default. In the following forum post Access Requests recipient by Default it is mentioned that in this case, i.e. when default email address someone@example.com is used, SPWeb.Site.Owner.Email will be used for sending access requests, although I didn’t check it by myself.

And one more thing: if you are working with CSOM note that Web.RequestAccessEmail property is available there only starting with version 16.1.4727.1200 onwards. Basically it means that it is not possible to change Access requests settings by CSOM for Sharepoint 2013 on-premise. It is still possible though via basic server object model.

Thursday, January 25, 2018

OfficeDevPnP classes and methods reference

Office 365 Developers Patterns and Practices library contains many useful methods for Sharepoint Online development. I would like to have some kind of reference page which would contain all methods from this doclib in one place so I can search them by Ctrl-F without pagination. In order to do such reference I used NDepend tool and built report from OfficeDevPnP.Core.dll v.2.22.1801.0 which is currently latest version for Sharepoint Online (note that for on-premise there is separate assembly). NDepend has useful feature called Code Query Linq (CQLinq) which allows you to make queries to the code in loaded assemblies, i.e. work with code as it would be data. E.g. for getting all methods with their parent classes I used the following Linq query:

   1: from m in Application.Methods
   2: let t = m.ParentType
   3: where !t.IsGeneratedByCompiler && !m.IsClassConstructor && !m.IsConstructor
   4: select new { m.ParentType, m }

After that exported report to Excel where added some formatting and saved to GitHub gist: https://gist.github.com/sadomovalex/74dc733c2b8a9a07a2ab23b7de705bf5.

You may use this gist as your reference for OfficeDevPnP as well.

Thursday, January 18, 2018

Enable possibility to download json files in Sharepoint

Sometime you may need to enable possibility to download json file types in Sharepoint (e.g. we had a case when it was needed for integration purposes – lobby screen at building reception used json file from Sharepoint site to show data to visitors). By default if you will put this file e.g. into /_layouts folder and try to access json file in context of Sharepoint site:

http://example.com/_layouts/test/foo.json

you will get the following error:

The page must have a <%@ webservice class="MyNamespace.MyClass" ... %> directive.
 
Stack trace:    at System.Web.UI.SimpleWebHandlerParser.ParseReader
()
   at System.Web.UI.SimpleWebHandlerParser.Parse(ICollection
referencedAssemblies)
   at System.Web.Compilation.SimpleHandlerBuildProvider.get_CodeCompilerTy
pe()
   at System.Web.Compilation.BuildProvider.GetCompilerTypeFromBuildProvider
(BuildProvider buildProvider)
   at System.Web.Compilation.BuildProvidersCompiler.ProcessBuildProviders()
   at System.Web.Compilation.BuildProvidersCompiler.PerformBuild()
   at System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath
virtualPath)
   at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal
(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean
allowBuildInPrecompile)
   at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert
(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean
allowCrossApp, Boolean allowBuildInPrecompile)
   at System.Web.UI.WebServiceParser.GetCompiledType(String inputFile,
HttpContext context)
   at System.Web.Services.Protocols.WebServiceHandlerFactory.GetHandler
(HttpContext context, String verb, String url, String filePath)
   at System.Web.Script.Services.ScriptHandlerFactory.GetHandler
(HttpContext context, String requestType, String url, String pathTranslated)
   at System.Web.HttpApplication.MaterializeHandlerExecutionStep.System.Web
.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step,
Boolean& completedSynchronously)

In order to enable possibility to download json file from Sharepoint we need to do the following:

1. Create new application sub folder under your Sharepoint site in IIS manager. If you already have virtual folder you need to convert it to application also in IIS manager. Separate folder is needed in order to not affect whole Sharepoint, but only one exact url:

http://example.com/testappfolder

2. Add web.config to this folder and add mime type record for json extension under system.webServer > staticContent:

   1: <configuration>
   2:    <system.webServer>
   3:       <staticContent>
   4:          <mimeMap fileExtension=".json" mimeType="application/json" />
   5:       </staticContent>
   6:    </system.webServer>
   7: </configuration>

3. In IIS manager open handler mappings for your application sub folder and remove handler for *.json (that's why it is important to use separate application sub folder in IIS – in case of app this change will only affect this sub folder, not whole Sharepoint site. I.e. there won’t be side effects with this approach).

Now if we try to access http://example.com/testappfolder/foo.json its content will be successfully downloaded.

Thursday, December 14, 2017

Problem with terminated site workflows which continue working in Sharepoint Online

Recently we faced with interesting issue in Sharepoint Online: there were several 2013 site workflows on different sub sites which worked this way:

  1. Iterate through publishing pages in Pages doclib of the current site and if current date is close to page’s Valid to date, workflow sends reminder email to responsible person
  2. Wait 1 day and then repeat iteration through pages

i.e. by itself workflow never ends. However it is possible to terminate workflow from UI: Site contents > Site workflows > click workflow > End workflow (see my previous post Restart site workflows in Sharepoint Online)

We published new version from Sharepoint Designer, ended previous workflow instances from UI and started new instances. And here we found interesting issue: terminated workflow instances continued to work, i.e. they continued to send emails of previous version to the users. In UI their status was displayed as Terminated. At the moment we contacted MS support but if you know about this issue please share it in comments.