Angular, CSOM, Office 365, pnp js core, SharePoint, SharePoint 2013, SharePoint 2016, SharePoint Event Receivers

Upload Docs to SharePoint Library & Update Columns using Pnp.js


Microsoft SharePoint Pattern and practices libraries makes it very simple to deal with SharePoint site. Less code and easy to understand. Link to SharePoint PNP-JS-Core.

In this article I will use pnp.js library to upload document and update the other fields in the same item uploaded in SharePoint document library.

(1 ) We need to reference the 4 files to work with pnp.js

/SiteAssets/4.0.5/es6-promise.js
/SiteAssets/4.0.5/es6-promise.auto.js
/SiteAssets/pnp.js
/SiteAssets/fetch.js

 

(2)I have reference the bootstrap in my page and use bootstrap classes.

Html of upload file, I have attached the link of Html Code in the PDF file, just copy and paste.

(3)  Now Call Simple JS Function UploadFiles() to Upload file and update the fields in the same item uploaded. Copy the below Code in JS File.

function UploadFiles() {
var files = document.getElementById(‘exampleInputFile’).files;
var file = files[0]; //using File Api
if (file!=undefined || file!=null){
$pnp.sp.web.getFolderByServerRelativeUrl(“/Documents”)
.files.add(file.name, file, true)
.then(function (data) {
var RelativeUrls=”/Documents/”+file.name;

//Retrive Document which is uploaded. Start
$pnp.sp.web.getFolderByServerRelativeUrl(RelativeUrls).getItem().then(item => {
//update start Below Call to Doc List will update the Row baseed on Item.ID
$pnp.sp.web.lists.getByTitle(“Documents”).items.getById(item.ID).update({
ColumnName: ‘New Value’
}).then(r => {
alert(file.name + ” upload successfully!”);
});
//update end
}); //Retrive Doc Info End
}); //Upload Document End

}

 

 

Below are the References to pnp js core library to deal with files and update item by id.  PDF file contain the html part.

Links

Standard
Office 365, SharePoint, SharePoint Event Receivers, SharePoint Provider hosted app

SharePoint Remote Event Receiver – Item Deleting – Using Provider hosted App


Scenario : We need to capture the person who delete the item in the document in SharePoint. On item deletion event will fire and add item to another list for further record. Code is attached and screen shorts are below step by step.

We have implemented a Remote Event Receiver on Document Library by Provider hosted app.

Pre-Requisites:(1) Office Developer Tool for VS2013  or VS2015, (2) Office 365 Subscription (3) Azure Subscription to host website over ssl.

  • First Create the SharePoint Add-In.
Select SharePoint Project

Select SharePoint Project

 

Select the SharePoint version

2

Select the SharePoint version

3

Choose the web project which will host the web service. As I am a big fan of MVC, So I select MVC.

 

4

 

SharePoint Will Create Two Project one is SharePoint ‚Äď Add in (App) which will reside in our App Catalog in our SharePoint.

Another web MVC project will host the web services, as SharePoint Event Remote Event Receivers use web services, Client Secret and Client ID to communicate for any event.

Now we will Add Events on our App Installing and App Uninstalling Events. I.e. Whenever App Install, our App will call web service to run one Process Method.

Right Click on our App Solution and Click for properties.

 

5

 

Once we Make Handle App Installed True, VS will create a AppEventServices.svc in our web project.

6

 

 

In AppEventReceivers.svc file a Process Methods Function we will Attached Event to the Doc Library on Item Deleting.

Below mentioned the Receiver Name, and the List Name on which we want to Add Event i.e. Document Library and the Destination List Name

7

Below mentioned is the Process Event Methods which run all the time.

8-b

 

In the Process Event Methods,

8

 

On App Installed Event i.e. when we add App to Site AppInstalled Event will occur. If Event not Exists than attached Event on ItemDeleting

 

 

 

9 10

If ItemDeleting occurs in the Document Library mentioned in the above than HandleItem event will occur.

11

 

12

 

Inside AddItem using CSOM to Add item to the List.

13

Now Build Solution, We are ready to register the website so that they will communicate with SharePoint using Client ID and Client Secret.

 

Go the App Catalog site and open the appregnew.aspx in the layouts.

 

 

14

 

Take the Client ID, Client Secret, and Title of the projects. App Domain the website name (The Azure site or any public hosting Site which can host this web).

 

Redirect url will be the default page of the site of hosted web.

 

To Host our web, we will use Azure which can communicate to SharePoint during item deleting event over ssl.

 

We will go to Azure web portal add new web.

 

15

 

Give any name. Export the publishing profile

 

16

 

For publishing profile, go the new created site, click on overview, Click on Get Publshing profile and export to your pc. This will help to publish the web from VS.

 

17 18 19

Click Create. This will give the details, save it we will need the Client ID and Client Secret to enter in the app and in the website to Communicate. By doing this SP (SharePoint) will know the handshake token.

 

20

 

Now Bind the app and the website using the Client ID and Client Secret. Open the AppMenifex.xml from app. Right Click on show code.

 

 

21

 

Enter the Client ID which we taken previously. This will alert SharePoint that whoever take this ID is can go next step.

 

22

Open the webconfig of website and enter the client secrect and client id.

 

23

 

Leave everything as it is.

 

Now publish the web to azure with the publishing profile.

 

24 25 26

Now publish app as .app file and upload to app catalog.

It will pop up to confirm the Client ID.

 

27

 

 

28

 

Summary.

 

Make sure that the below List and Document Library should exists on the Destination site.

 

29

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.EventReceivers;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace VDC_AppWeb.Services
{
public class AppEventReceiver : IRemoteEventService
{

private const string ReceiverName = “ItemDeleting”;
private const string ListName = “VDC Documents”;
private const string DestiNationList = “DestList”;
public SPRemoteEventResult ProcessEvent(SPRemoteEventProperties properties)
{

SPRemoteEventResult result = new SPRemoteEventResult();
switch (properties.EventType)
{
case SPRemoteEventType.AppInstalled:
HandleAppInstalled(properties);
break;
case SPRemoteEventType.AppUninstalling:
HandleAppUninstalling(properties);
break;
case SPRemoteEventType.ItemDeleting:
HandleItem(properties);
break;
}
return result;
}

private void HandleAppInstalled(SPRemoteEventProperties properties)
{
using (ClientContext clientContext = TokenHelper.CreateAppEventClientContext(properties, false))
{
if (clientContext != null)
{
List myList = clientContext.Web.Lists.GetByTitle(ListName);
clientContext.Load(myList, p => p.EventReceivers);
clientContext.ExecuteQuery();
bool rerExists = false;
foreach (var rer in myList.EventReceivers)
{
if (rer.ReceiverName == ReceiverName)
{
rerExists = true;
System.Diagnostics.Trace.WriteLine(“Found existing ItemDeleted receiver at ” + rer.ReceiverUrl);
}
}
if (!rerExists)
{
EventReceiverDefinitionCreationInformation receiver = new EventReceiverDefinitionCreationInformation();
receiver.EventType = EventReceiverType.ItemDeleting;
OperationContext op = System.ServiceModel.OperationContext.Current;
Message msg = op.RequestContext.RequestMessage;
receiver.ReceiverUrl = msg.Headers.To.ToString();
receiver.ReceiverName = ReceiverName;
receiver.Synchronization = EventReceiverSynchronization.Synchronous;
myList.EventReceivers.Add(receiver);
clientContext.ExecuteQuery();
System.Diagnostics.Trace.WriteLine(“Added ItemDeleted receiver at ” + msg.Headers.To.ToString());
}
}
}
}

private void HandleAppUninstalling(SPRemoteEventProperties properties)
{
using (ClientContext clientContext =
TokenHelper.CreateAppEventClientContext(properties, false))
{
if (clientContext != null)
{
List myList = clientContext.Web.Lists.GetByTitle(ListName);
clientContext.Load(myList, p => p.EventReceivers);
clientContext.ExecuteQuery();

var rer = myList.EventReceivers.Where(
e => e.ReceiverName == ReceiverName).FirstOrDefault();

try
{
System.Diagnostics.Trace.WriteLine(“Removing ItemDeleted receiver at ”
+ rer.ReceiverUrl);

//This will fail when deploying via F5, but works
//when deployed to production
rer.DeleteObject();
clientContext.ExecuteQuery();

}
catch (Exception oops)
{
System.Diagnostics.Trace.WriteLine(oops.Message);
}

}
}
}

private void HandleItem(SPRemoteEventProperties properties)
{
using (ClientContext clientContext = TokenHelper.CreateRemoteEventReceiverClientContext(properties))
{
if (clientContext != null)
{
try
{
List photos = clientContext.Web.Lists.GetById(properties.ItemEventProperties.ListId);
ListItem item = photos.GetItemById(properties.ItemEventProperties.ListItemId);
// FieldUserValue fvv2 = (FieldUserValue);

clientContext.Load(item);
clientContext.ExecuteQuery();
string Titles = Convert.ToString(item[“FileLeafRef”]);
string Location = Convert.ToString(item[“FileRef”]); ;
string FinalLocation = Location.Replace(Titles, ” “);
string CreatedBys = properties.ItemEventProperties.UserDisplayName;
item.Update();
clientContext.ExecuteQuery();

AddItem(Titles, CreatedBys, FinalLocation, properties);
}
catch (Exception oops)
{
System.Diagnostics.Trace.WriteLine(oops.Message);
}
}
}
}
private void AddItem(string Title, string User, string Location, SPRemoteEventProperties properties)
{

using (ClientContext clientContext = TokenHelper.CreateRemoteEventReceiverClientContext(properties))
{
if (clientContext != null)
{
List oList = clientContext.Web.Lists.GetByTitle(“VDC Delete Info”);

ListItemCreationInformation listItemCreationInfo = new ListItemCreationInformation();
ListItem oListItem = oList.AddItem(listItemCreationInfo);
oListItem[“Title”] = Title;
oListItem[“DeleteBy”] = User;
oListItem[“Location”] = Location;
oListItem.Update();
clientContext.ExecuteQuery();
}
}
}

/// <summary>
/// This method is a required placeholder, but is not used by app events.
/// </summary>
/// <param name=”properties”>Unused.</param>
public void ProcessOneWayEvent(SPRemoteEventProperties properties)
{
throw new NotImplementedException();
}

}
}

  • ¬†

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Standard
Uncategorized

Last Day @ NBAD- Tweeeted


Sometime you sense very dissimilar like you oversight something in your life. Same feeling I am having due to my last day at NBAD. Aimed at short period of time I encountered a team which has good strength and outstanding technical communication.

A team with calm and diminish observance. I will never forget this miles stone in my life, even though I have been a slice of numerous professional team but occupied with this team like additional  work without knowing that you are burdened  

Standard
Search Service application, SharePoint 2013, SharePoint 2016, Sharepoint2010

SharePoint 2013 Search Administration Web Service Application stopped- Stuck


Recently I got stuck with the SharePoint Search Service application. It was deleted earlier from the Farm. But Search Administration Web  Service Application is stuck with stopped status.

Search Service Web application status Stopped

Search Service Web application status Stopped

Solution:
Below Power shell Command will successfully remove Search remaining stuff form the Farm. with all the web services.
# Add SharePoint cmdlets reference
Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue
$SSA = Get-SPEnterpriseSearchServiceApplication
$SSA.Delete()

Standard
Uncategorized

UPS Synchronization Service- SharePoint Server 2016, Microsoft Identity Manager?


Recently I configured SharePoint 2016 Review release to have a deep look at the new changes contrary to 2013. During configuring user profile services I dint find User profile service Synchronization in services on server only user profile services, I checked the FIM has been removed in favor of Microsoft Identity Manager.

ups1

 

 

MIM is a separate server technology (not built-in to SharePoint Server). That means, if you have MIM running in your company, more than one SharePoint Server 2016 farm can rely upon it.

Active Directory Import (sometimes called Active Directory Direct Import) is also included with SharePoint Server 2016, and is a User Profile Synchronization alternative that will not need a separate server installation. This means that SharePoint Server 2016 offers two options for User Profile Sync.

ups2

ups2

 

Source : https://technet.microsoft.com/en-us/library/mt627723(v=office.16).aspx

 

Standard
Javascript Object Model SharePoint, SharePoint 2016

SharePoint 2016 ‚ÄėHTTP 500 Internal Server Error‚Äô while trying to browse to a SharePoint site using Claims-based


I got a chance to Install SharePoint 2016 SharePoint Server 2016 Preview and Configure Single Server Farm (SharePoint Server and another is SQL Server). Every thing went good but Once I launch a web application HTTP 500 Internal Server Error from web application. 

I check the Event Viewer below erorr adding in the Event Viewer.

 

 

Security Token Issuer

Security Token Issuer

Resolution:

I checked the SharePoint web services It was pointing to 15 hive folder. Change it to 16 it works.

Error2

 

Reason for sharing to add in to my thoughts diaries and I hope it may help others.

Note: I checked the 2016 and most of the item are similar to 2013. I will deploy some web part, install workflow manager and initiate some workflows and share my thoughts.

 

 

 

Standard
5000 Threshold, CSOM, Javascript Object Model SharePoint, SharePoint 2013, SharePoint List Lookup, Sharepoint2010, Uncategorized

Get 50000 items from SharePoint List using JS-SharePoint Item 5000 Threshold Issue [Solved] –


My blog will help to load data from SharePoint list regardless of list-view threshold limit . I have been through many forums and even I experienced bit difficulty to load data from list having record greater than limit defined by the SharePoint administrator (threshold limit). I have created a simple JS file with some functions to load data in chunks. i.e. My JS function will automatically take the total count of list and break them in the chunks and iterate them. In the result it will provide the array of items requested by the user.

Global Varaiable

Screen shot is to provide the global variable for our JS File

Initial Method to Call from my js

Initial Method to Call from my js

Internal method of JS File, This methods will take the cound of the List and divide them as 5000 each cycle

Internal method of JS File, This methods will take the cound of the List and divide them as 5000 each cycle

 

 

 

Final Methods will runs once all the data finish loading

Final Methods will runs once all the data finish loading

Just add the attached JS file to your SharePoint Solution and call the below mentioned function. In return once the data get loaded , it will write the desired array to flatArray and update var TransactionComplete = 1; further you can use flatArray with data in your app.
JS File – Click here to Download – Rename PDF to JS

Funciton to call GetListItem(siteurl,listname,columns)

siteurl = http://sitename

listname=testlist

columns=ID,Created,Title,Column1,Column2,Column4,Column5

Result = flatArray as Array

Resultant Screen shot.

ResultArray

ResultArray

 

I hope my little efforts may save time for those who are struggling with SharePoint threshold 5000 issue.

Standard