Tuesday 4 October 2022

Trial period reset of Visual Studio Community Edition 2017, 2019

Trial period reset of Visual Studio Community Edition 2017 , 2019


Download Source code (zip) from this Link : https://github.com/1Dimitri/VSCELicense/releases/tag/1.0

  1. Run PowerShell.exe as an Administrator

  2. Import module:

Import-Module -Name E:\vs2019\VSCELicense


For Visual Studio Community Edition 2019


Get Visual Studio Community Edition license expiration date

Get-VSCELicenseExpirationDate -Version VS2019

Set license expiration date to current date + 30 days


Set-VSCELicenseExpirationDate -Version VS2019 -AddDays 30


For Visual Studio Community Edition 2017


Get Visual Studio Community Edition license expiration date

Get-VSCELicenseExpirationDate -Version VS2017

Set license expiration date to current date + 30 days


Set-VSCELicenseExpirationDate -Version VS2017 -AddDays 30


Wednesday 7 September 2022

Add DLL to SharePoint bin folder

Add DLL to SharePoint bin folder

 I have done this before for some sharepoint 2013 webparts projects and when googling I found this article, there it is explained how to sign third party assemblies with strong name. Basically you disassemble the dll first with ILDASM.exe and then you reassemble with ILASM.exe providing a *.snk file previously done with sn.exe. All this is explained in detail in the given article.

https://docs.microsoft.com/en-us/archive/blogs/miah/how-to-assign-a-strong-name-to-an-unsigned-3rd-party-assembly

How to sign a .NET Assembly DLL file with a Strong Name

 How to sign a .NET Assembly DLL file with a Strong Name 

.NET Assembly DLL file can be sign with strong name if it is no sign by using ILDASM and ILASM

Create strong name file.

C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools

sn -k sgKey.snk


(Dis)assemble a dll using the Visual Studio command prompt


ildasm directory\myLibrary.dll output:directory\MyLibrary.il

Assemble a dll using the Visual Studio command prompt

ilasm c:\myLibrary.il /dll /key=C:\MessageIntegrity.snk /output:C:\myLibrary.dll


another example :
> ildasm /all /out=MYASSEMBLY.il MYASSEMBLY.dll
> ilasm /dll /key=key.snk MYASSEMBLY.il






Create a strong name key (snk) file

 

Create a strong name key (snk) file


Click Start, point to Programs, point to Microsoft Visual Studio 2019, point to Visual Studio Tools, and then click Visual Studio Command Prompt (2019).

  • At the command prompt, navigate to the location where you want to create the key file. For example, type cd C:\Sample, and then press ENTER.

  • At the command prompt, type sn -k <key file name>.snk, and then press ENTER.

  • At the command prompt, type exit, and then press ENTER.

Wednesday 31 August 2022

Calculate drive space using powershell

Following command use to calculate drive available space 

 Get-WmiObject -Class Win32_logicaldisk -Filter "DriveType = '3'" | 

Select-Object -Property DeviceID, DriveType, VolumeName, 

@{L='AvailableSpace';E={"{0:N2}" -f ($_.FreeSpace /1GB)}},

@{L="TotalDiskSize";E={"{0:N2}" -f ($_.Size/1GB)}}

Tuesday 26 October 2021

SharePoint 2013 / 2016 : Associate a Workflow with a List or Library

SharePoint 2013  / 2016 : Associate a Workflow with a List or Library 

Scenario/Problem: You want to assign a workflow to a list or library so that users can select it from the list of available workflows, or you might want to define a workflow that starts automatically when a document or list item is added or modified in the library or list.

Solution: To attach different workflows to a list or library, you need the Manage List or Manage Library permission on that list or library. If you have those permissions, switch to the Library ribbon or List ribbon and click on the Workflows button (see Figure 1).


FIGURE 1 The Workflows button in the Library ribbon.

Clicking the Workflows button opens the Workflow Settings page for the list or library. This page enables you to associate a workflow with the list or library (see Figure 2).


FIGURE 2 The Workflow Settings page for adding a workflow or managing the existing ones.

The list of workflows that were already added to the list or library appears at the top of the page (under the Workflows title). The option to add a workflow is shown as a link at the bottom of the page. Click this link to open the Add a Workflow page, shown in Figure 3.


FIGURE 3 The Add a Workflow page.

In the Add a Workflow page, you can define what workflow you want to create on the list or library by selecting from the workflow box that lists the available workflows. By default, the only workflows available in SharePoint Server are the five built-in workflows:


Finally, the last section of the page, Start Options, enables you to specify when the workflow starts (see Figure 4).









Monday 20 September 2021

How to Add Remote Event Receivers to the Host Web List SharePoint

 In the previous post, we looked at the process of adding and debugging of Remote Event Receiver. The receiver was added to the list, which is set in our Web application.

In this post we will take a look at how to add event receivers to the lists that are in the Host Web.

As an example, we will create a Provider-hosted application (app). This application will allow us to connect the remote event receiver to any list in the Host Web.

First step: create a list (Custom List) and name it Test.



Then create Provider-hosted app in Visual Studio (as in the previous post). So we created a solution that consists of two projects (app + website).






Now add the Remote Event Receiver. Right click on the project name RERHostDemo -> Add -> New Item … select Remote Event Receiver.

Let’s name it RERHostReceiver. Then click Add.



Next step: select List Item Events, as a source of Custom List, as well as select the event, on which should trigger our receiver – An item was added. Click Finish



Both of our project will be modified. The project RERHostDemo reveal element RERHostReceiver, then click on Elements.xml to view its contents.


We see that this receiver will be connected to all lists with a template Custom List (ListTemplateId = 100). Now open the second draft RERHostDemoWeb, code file receiver – RERHostReceiver.svc.cs. Now we set up the breakpoint in ProcessOneWayEvent method and run the application in debug mode – press F5 (Do not forget to set the service bus and endpoint for RERHostDemo project. How to do this, see the previous post).

After launching the application, appears the window asking for permission. Click Trust It.


Then we are redirected to the page of our web application. Now this is a normal MVC application without any changes.



Without closing the tab (or Visual Studio will be out of debug mode), open another tab and go to your portal. Open the test list and use it to create the element. Our breakpoint will not work.
Let’s take a look at the Test list of remote receiver.
To do this, open the Powershell ISE and run the following code:


function listEventReceivers([Microsoft.SharePoint.Client.ClientContext]$context, [string]$listName)   
 {  
   $list = $context.Web.Lists.GetByTitle($listName);   
   $context.Load($list)  
   $eventReceivers = $list.EventReceivers  
   $context.Load($eventReceivers)  
   $context.ExecuteQuery()  
   foreach ($er in $eventReceivers)  
   {  
     Write-Host ("Found ER: " + $er.ReceiverName)  
     Write-Host ("ReceiverClass: " + $er.ReceiverClass)  
     Write-Host ("ReceiverAssembly: " + $er.ReceiverAssembly)  
     Write-Host ("EventType: " + $er.EventType)  
     Write-Host ("ReceiverUrl: " + $er.ReceiverUrl)  
     Write-Host ("Synchronization: " + $er.Synchronization)  
   }  
 }  
 $username = "test"   
 $password = "test"   
 $url = "https://test.sharepoint.com"  
 $securePassword = ConvertTo-SecureString $password -AsPlainText -Force  
 $listName = "Test"  
 Add-Type -Path "c:Program FilesCommon Filesmicrosoft sharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.dll"   
 Add-Type -Path "c:Program FilesCommon Filesmicrosoft sharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.Runtime.dll"   
 $clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)   
 $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)   
 $clientContext.Credentials = $credentials  
 if (!$clientContext.ServerObjectIsNull.Value)   
 {   
   listEventReceivers $clientContext $listName  
 }   


As the result of the executed code, the screen will be empty, since in the list of Test does not have event receivers (for detailed info about the above code – here).

The point is that the markup in RERHostReceiver (Elements.xml) file connects only to our receiver lists in the app web. Declaratively, connection of remote event receivers to lists on Host Web is not possible. But we can do it programmatically.
First, open AppManifest.xml file (in RERHostDemo project), then go to the tab Permissions. In order to enable us to add event receivers from the code, it is necessary for user to have the right to Manage Host Web. In the Scope column set Web, and Permissions column Manage value.


Now remove RERHostReceiver element from the RERHostDemo project, and leave the code of the receiver in the project RERHostDemoWeb (we will use it later).

In RERHostDemoWeb project open the file Index.cshtml and replace its contents with the following code:

 @{  
   ViewBag.Title = "Home Page";  
 }  
 <div class="row">  
   @using (Html.BeginForm("Subscribe", "Home", new { SPHostUrl = Request.QueryString["SPHostUrl"] }))  
   {  
   @Html.AntiForgeryToken()  
   <div class="col-md-10">  
     @Html.DropDownList("listTitle", (IEnumerable<SelectListItem>)ViewBag.HostLists)  
     <input type="submit" value="Subscribe" />  
   </div>  
   }  
 </div> 


Now open HomeController.cs file, change the code of the Index method to the following:

[SharePointContextFilter]  
 public ActionResult Index()  
 {  
   var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);  
   using (var clientContext = spContext.CreateUserClientContextForSPHost())  
   {  
     if (clientContext != null)  
     {  
       var spWeb = clientContext.Web;  
       var hostListColl = spWeb.Lists;  
       clientContext.Load(spWeb, w => w.Id);  
       clientContext.Load(hostListColl);  
       clientContext.ExecuteQuery();  
       ViewBag.HostLists = hostListColl.Select(l => new SelectListItem() { Text = l.Title, Value = l.Title });  
     }  
   }  
   return View();  
 }  

After launching the application, we will see a drop-down list filled with lists of names from Host Web.


By clicking on the Subscribe button, Subscribe action of the Home controller will be triggered. We have not yet written code for it, so click on the button does not make sense.

Let’s go back to Visual Studio. Add new class RERUtility to RERHostDemoWeb project with the following code:

 using Microsoft.SharePoint.Client;  
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 namespace RERHostDemoWeb  
 {  
   public class RERUtility  
   {  
     public static void AddListItemRemoteEventReceiver(ClientContext context, string listName, EventReceiverType eventType,  
       EventReceiverSynchronization synchronization, string receiverName,  
       string receiverUrl, int sequence, string receiverAssemblyName = "", string receiverClassName = "")  
     {  
       var list = context.Web.Lists.GetByTitle(listName);  
       context.Load(list);  
       var eventReceivers = list.EventReceivers;  
       context.Load(eventReceivers);  
       context.ExecuteQuery();  
       var newRER = new EventReceiverDefinitionCreationInformation();  
       newRER.EventType = eventType;  
       newRER.ReceiverName = receiverName;  
       newRER.ReceiverClass = receiverClassName;  
       newRER.ReceiverAssembly = receiverAssemblyName;  
       newRER.ReceiverUrl = receiverUrl;  
       newRER.Synchronization = synchronization;  
       newRER.SequenceNumber = sequence;  
       list.EventReceivers.Add(newRER);  
       list.Update();  
       context.ExecuteQuery();  
     }  
   }  
 }  

In this class, we created a method that gets the list by name and adds to it the remote event receiver.
Re-open HomeController.cs file and add the following code:

[SharePointContextFilter]  
     [HttpPost]  
     public ActionResult Subscribe(string listTitle)  
     {  
       var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);  
       using (var clientContext = spContext.CreateUserClientContextForSPHost())  
       {  
         if (!string.IsNullOrEmpty(listTitle))  
         {  
           RERUtility.AddListItemRemoteEventReceiver(  
             clientContext,  
             listTitle,  
             EventReceiverType.ItemAdded,  
             EventReceiverSynchronization.Asynchronous,  
             "RERHostReceiver",  
             "",  
                 10);  
         }  
       }  
       return RedirectToAction("Index", new { SPHostUrl = spContext.SPHostUrl.ToString() });  
     } 


Pay attention to (“”) value in the penultimate parameter in AddListItemRemoteEventReceiver method. In this setting, we need to pass a reference to our receiver, namely, on the service responsible for events processing. If our service was published online, then we would know its address. But what if we want to debug receiver code without publishing (to localhost)?
The answer – we need to register the service address in the service bus.
Select RERHostDemo project and press F4, in the appeared properties window, set True in Handle App Installed.


Visual Studio automatically creates a handler class in the folder Services, which will be triggered when you install our application. Open AppEventReceiver.svc (project RERHostDemoWeb).
Replace the code of the ProcessEvent method to the following:

 public SPRemoteEventResult ProcessEvent(SPRemoteEventProperties properties)  
 {  
    SPRemoteEventResult result = new SPRemoteEventResult();  
    var serviceUrl = System.ServiceModel.OperationContext.Current.Channel.LocalAddress.Uri.ToString();  
    return result;  
 }  

Set the breakpoint on line on the last line. Press F5 to run the solution in debug mode. This will open a browser window with information on the requested rights; click Trust It. Wait until our receiver launches (it may take a few seconds). Once the code execution stops, look at the contents of serviceUrl variable. It contains service address, which is responsible for receiving the event when the application is installing. Copy and save it in notepad.


Exit debug mode, open HomeController.cs file, go to Subscribe method. Replace the value of the (“”) penultimate parameter to the service address that you saved in Notepad. At the end of the address, replace AppEventReceiver.svc to the name of your service – RERHostReceiver.svc.
You can safely remove AppEventReceiver service. Also, do not forget to set false value in the property of the RERHostDemo project for Handle App Installed.

The following example creates a receiver that will be triggered asynchronously to the «Item was added» event. Now open RERHostReceiver.svc.cs file and set breakpoint in ProcessOneWayEvent method. Press F5. Select list, to which we want to add our receiver, click Subscribe.

Open another tab in your browser and go to the list, to which we added the receiver. Create a new element. Hooray, our breakpoint load.



ps: Unfortunately, at the moment it is impossible to remove the event receiver that are added «through service bus». An error «Access Denied» occurs. The only way  is to delete the list. Receivers, written by published service (in Azure) can be removed properly. Here you can see examples of code for removing/ adding/etc. event receivers.