Enable empty SP search textbox does not update results


Hi Guys,

This post is about enabling empty search queries. By default, when user press ‘Enter’ on an empty Search Box, nothing happens.  A very simple question might be asked that why would anyone search for empty string any way?  True, no one actually needs it desperately but while going in flow this case could cause user bed experiences because users are likely to start with the refiners.

Problem:

Consider this flow:

  1. Navigate to SharePoint Search page
  2. Search with any keyword like “Test”
  3. If search results are not relevant, I just removed Test keyword from the textbox and hit search button.
  4. Nothing actually happens here. It suppose to clear all existing results.

Without the ability to search for the empty string (or a “clear” button), the only way to do same would be to reload the page.

Solution: 

The Search Box webpart supports empty string search. Yes, its true, it’s just disabled by default.  This feature is controlled by a property, AllowEmptySearch, which can not be managed from the UI. This fix is rather easy. This is a property that can be set on the search box, by default set to false. Follow the given instructions to update this property:

  1. Edit the page where search box is placed.
  2. Export your Search Box webart.
  3. Edit the .webpart file using your favorite text editor
  4. Search for the property AllowEmptySearch and update its value to True.
  5. Now import this web part to gallery and add it to your page in place of default search box web part.

This functionality is necessary when you have pages on which you configured the search web parts to use predefined queries in order to show results when the page gets loaded.

When you update this property to true, which can be done by exporting the search box web part, and changing the following line in the XML:

<! old >
<property name=“AllowEmptySearch” type=“bool”>False</property>
<! new >
<property name=“AllowEmptySearch” type=“bool”>True</property>

Note: you can also update the web part with PowerShell.

Another way to approach it is via setting this property from within your display template. To update the property within the search box display template, you have to add the following lines of code:

// Allow empty keyword searches
if (!ctx.ClientControl.get_allowEmptySearch()) {
    ctx.ClientControl.set_allowEmptySearch(true);
}

Hope this small trick helps you solve this issue.

Happy SharePointing !

Ref: Enable empty SPsearch textbox does not update results

Advertisements

Add welcome pop up for first time users of your site using AngularJs Material


Hi Guys,

We really do only have one chance for putting a first impression for end users on our website. User’s will never land back to your website if there is nothing interesting found on web site. Same logic works for SharePoint sites too. When a visitor lands on your site for the first time (and maybe the last), how you welcome them will have a big impact. This can be accomplished with pop-up boxes, fullscreen welcome mats, live chat invitations, and more.

This post is all about a welcome popup on SharePoint site. In brief, I try my best to provide very easy solution of showing welcome pop up.

Requirement: One of my recent project client asked to show a welcome message. Server side webarts are not promoted by out clients because our site was built in SP online. It sounds very easy but complication added by showing this pop up only once per user.

Solution: As a solution, i suggested to achieve this using Content editor web part. Just place some quick JS code on the landing page and with the help of a list we are able to detect if user is first time user or not. So here is the components of this solution:

  1. A list “Users” which stores list of first time users
  2. Content editor web part.
  3. Site Assets library to refer the local references like Angular, Jquery, css etc.

Let me go through step by step implementation of this solution.

Step I: Create a “Users” list

  1. Visit Site > Site contents.
  2. Click on New > Choose Custom List template> Name it as “FirstTimeUsers”.
  3. Once list is created. Navigate to newly created List settings > Add column
  4. Add column “User” of type “Person and Group”.
  5. Now with this list Title field is required. We can make this field hidden or optional if not required.

We are done with creating a list which stores information about first time users. We will be updating using rest api calls and each time when user logged on to site. His/her entries will be added to this list only.

Step II: Add references for angular, material, jquery etc.

This step can be skipped in case you need to refer all required libraries from CDN. But I never recommend this way, rather download the latest files to Site assets library. It also reduces extra burden on your site to download these library each time from CDN.

  1. Navigate to the Site assets library.
  2. Create a folder called “CustomFiles”.
  3. Create another folder “AngularJS”.
  4. Download files angular.min.js, angular-animate-min.js, angular-aria.min.js, angular-material.min.css, angular-material.min.js, angular-messages.min.js, jquery-3.3.1.min.js
  5. Upload these files to AngularJs folder.   

Step III: Create angularJS app files

  1. Create two files named as: dialog.tmpl.html and dialogApp.js
  2. Copy following code to dialog.tmpl.html file: dialog_tmpl_html
  3. Copy following code to dialogApp.js: dialogapp_js
  4. Now change the <Site URL> with the url of your site.

These two files are required for angularJS to initialize and configure to show dialog box. The html file can be used to modify contents of dialog box.

Step IV: Add content editor webpart on homepage.

  1. Navigate to the page where you need to show pop up
  2. Edit this page and click on add webpart under any zone
  3. Under “Media and content” find “Content Editor” web part. and add it.
  4. Now Edit this web part and under ribbon find “Format text” tab and “Edit Source” option under it.
  5. Now past following code here:

http://SiteURL/SiteAssets/customfiles/angularjs/angular.min.js

http://SiteURL/SiteAssets/customfiles/angularjs/angular-animate.min.js

http://SiteURL/SiteAssets/customfiles/angularjs/angular-aria.min.js

http://SiteURL/SiteAssets/customfiles/angularjs/angular-messages.min.js

http://SiteURL/SiteAssets/customfiles/angularjs/angular-material.min.js

http://SiteURL/SiteAssets/customfiles/dialogApp.js
http://SiteURL/SiteAssets/customfiles/angularjs/jquery-3.3.1.min.js
<link href=”<SiteURL>/SiteAssets/customfiles/angularjs/angular-material.min.css” rel=”stylesheet”/>

​​​​​​​

</div>

That is it. Just save your page and Publish it.

Step V: Test your pop up.

Now log on with separate users on the home page and check if there is entry created under “FirstTimeUsers” list.

Let me explain here a little about the ajax calls here. The code under dialogApp.js files first get the information of the logged on user. Get user information SharePoint using REST Api. Then using REST api, it tries to find out its entry under “FirstTimeUsers” list. Here is the rest call:

/_api/web/lists/getByTitle(‘FirstTimeUsers’)/items?$select=User/EMail&$expand=User&$filter=User/EMail eq ‘<email_of_user>’

In case, it found any entry popup is not shown to user. But if it has found any entry then pop up is shown and on close event of the pop up another rest query will insert entry to the FirstTimeUsers List.

That is all about it. Don’t forget to hit like and kindly rate this article.

Happy SharePointing !!!

 

Get user information SharePoint using REST Api


Hi,

While working on a requirement, I encountered a scenario where I need to show welcome pop up to the first time users. First time user, SharePoint does not provide any api or any solution which gives information about users who logged on first time on your web site. I have documented the whole solution on some other post(Add welcome pop up for first time users of your site using AngularJs Material) But this post I only want to scope it to retrieve information of logged on user. Information mean current user’s accountId, First name, Last name, Email and other required details.

I am only talking about client side code here i.e. using REST api or SP default APIs. Below are the various ways to get the property of current user who is logged on to SP site. There are many ways to achieve this requirement but I prefer to use SP.UserProfiles.PeopleManager Api to get it. It gives us vast option to retrieve any user information. Here are some details of getting various details:

  1. Get all properties of current user:

    http://siteurl/_api/SP.UserProfiles.PeopleManager/GetMyProperties

  2. Get single property of current user:

    http://siteurl/_api/SP.UserProfiles.PeopleManager/GetMyProperties/PictureUrl

    OR

    http://siteurl/_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=PictureUrl

  3. Get Multiple Properties for the current user:

    http://siteurl/_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=PictureUrl,AccountName

  4. Get all properties of Specific User:
    For Office 365/SharePoint Online:

    http://siteurl/_api/SP.UserProfiles.PeopleManager/GetPropertiesFor(accountName=@v)?@v=’i:0%23.f|membership|mohit.vashishtha@siteurl.onmicrosoft.com’

    For SharePoint 2013 On-Premise:

    http://siteurl/_api/SP.UserProfiles.PeopleManager/GetPropertiesFor(accountName=@v)?@v=’domain\username’

  5. Get Specific UserProfile Property of Specific User:
    For Office 365/SharePoint Online:

    http://siteurl/_api/SP.UserProfiles.PeopleManager/GetUserProfilePropertyFor(accountName=@v,propertyName=’LastName&#8217;)?@v=’i:0%23.f|membership|mohit.vashishtha@siteurl.onmicrosoft.com’

    For SharePoint 2013 On-Premise:

    http://siteurl/_api/SP.UserProfiles.PeopleManager/GetPropertiesFor(accountName=@v)?@v=’domain\username’

NOTE: I had issues with getting the specific property for a user on #5. This worked fine though:

http://siteUrl/_api/SP.UserProfiles.PeopleManager/GetPropertiesFor(accountName=@v)?@v=’domain\mohit.vashishtha’&$select=DisplayName,AccountName,PictureUrl

Here are some details about user properties:

List of User Properties (Use the GetPropertiesFor function for these):

  • AccountName
  • DirectReports
  • DisplayName
  • Email
  • ExtendedManagers
  • ExtendedReports
  • IsFollowed
  • LatestPost
  • Peers
  • PersonalUrl
  • PictureUrl
  • Title
  • UserProfileProperties
  • UserUrl

List of User Profile Properties (Use the GetUserProfilePropertyFor function for these):

  • AboutMe
  • SPS-LastKeywordAdded
  • AccountName
  • SPS-Locale
  • ADGuid
  • SPS-Location
  • Assistant
  • SPS-MasterAccountName
  • CellPhone
  • SPS-MemberOf
  • Department
  • SPS-MUILanguages
  • EduExternalSyncState
  • SPS-MySiteUpgrade
  • EduOAuthTokenProviders
  • SPS-O15FirstRunExperience
  • EduPersonalSiteState
  • SPS-ObjectExists
  • EduUserRole
  • SPS-OWAUrl
  • Fax
  • SPS-PastProjects
  • FirstName
  • SPS-Peers
  • HomePhone
  • SPS-PersonalSiteCapabilities
  • LastName
  • SPS-PersonalSiteInstantiationState
  • Manager
  • SPS-PhoneticDisplayName
  • Office
  • SPS-PhoneticFirstName
  • PersonalSpace
  • SPS-PhoneticLastName
  • PictureURL
  • SPS-PrivacyActivity
  • PreferredName
  • SPS-PrivacyPeople
  • PublicSiteRedirect
  • SPS-ProxyAddresses
  • QuickLinks
  • SPS-RegionalSettings-FollowWeb
  • SID
  • SPS-RegionalSettings-Initialized
  • SISUserId
  • SPS-ResourceAccountName
  • SPS-AdjustHijriDays
  • SPS-ResourceSID
  • SPS-AltCalendarType
  • SPS-Responsibility
  • SPS-Birthday
  • SPS-SavedAccountName
  • SPS-CalendarType
  • SPS-SavedSID
  • SPS-ClaimID
  • SPS-School
  • SPS-ClaimProviderID
  • SPS-ShowWeeks
  • SPS-ClaimProviderType
  • SPS-SipAddress
  • SPS-ContentLanguages
  • SPS-Skills
  • SPS-DataSource
  • SPS-SourceObjectDN
  • SPS-Department
  • SPS-StatusNotes
  • SPS-DisplayOrder
  • SPS-Time24
  • SPS-DistinguishedName
  • SPS-TimeZone
  • SPS-DontSuggestList
  • SPS-UserPrincipalName
  • SPS-Dotted-line
  • SPS-WorkDayEndHour
  • SPS-EmailOptin
  • SPS-WorkDayStartHour
  • SPS-FeedIdentifier
  • SPS-WorkDays
  • SPS-FirstDayOfWeek
  • Title
  • SPS-FirstWeekOfYear
  • UserName
  • SPS-HashTags
  • UserProfile_GUID
  • SPS-HireDate
  • WebSite
  • SPS-Interests
  • WorkEmail
  • SPS-JobTitle
  • WorkPhone
  • SPS-LastColleagueAdded

Happy SharePointing !!!

Reassign a workflow task – Task update was not accepted. To edit a task associated with a workflow task process, you must be the person to whom the task is currently assigned.


Hi guys,

Recently one of my client has faced this issue while reassigning a task of default Approval workflow. Here is the screen shot:

      

SharePoint by default only allows the assignee to re-assign the task. If you try to re-assign a task that is not assigned to you, you get the following message:

Task update was not accepted. To edit a task associated with a workflow task process, you must be the person to whom the task is currently assigned.

Solution

To overwrite this behaviour:

  1. Open SharePoint Designer
  2. Open the Workflow you wish to modify.
  3. On the step where you start the workflow task ‘Start <workflow task name> process on Current Item with…’, right click on the task name.
  4. Select Properties and look for the attribute TaskProcessOwner.
  5. Click on the ‘‘ button to select a group or specific user.
  6. You may wish to create a SharePoint group specifically for this purpose. All members of this group will have permissions to re-assign tasks that are assigned to other people.

Hope this post help you to fix this error.

Happy SharePointing.

SharePoint Online/2013 Replace no results default text ‘Nothing here matches your search’ from search results web part


 

As we all aware, SharePoint search have great potential to provide very powerful search solution. Couple of new webparts are added in SharePoint 2013, like Content Search web part for which I should use rather a word “Smart” search web part. With a new rendering mechanism, content search webpart becomes more and more simple to edit and customize. That mechanism is called “Display Templates” and these are based upon simple Json to HTML templates.

I recently has faced a project scenario, where I was asked to replace the part of search result web part and I provided a solution by updating search display template.

Requirement:  When we add a search result web part on any result page along with a search text box web part to enter search keywords. Search results will be displayed in web part if keyword matches any text. When we add both these web parts on page and before entering the search term also the results web part show  “Nothing here matches your search” (as shown in below image), which my users want to modify based on their content and want to add some useful links into it.

Blog1

 

Solution:

Initially, I thought that this may be easily updated using some web part property, but after researching about it, I found that there are html/json files called, display templates, which are responsible for rendering this text. Let me explain how to find them and which Display Template we need to customize in order to achieve our target.

All kind of Display Templates are stored at the one location under master page gallery. Here are the steps to reach out to the html file.

  1. Open the site collection.
  2. Navigate to Site Settings.
  3. Under Web Designer Galleries click on Master pages and page layoutsBlog2
  4. Now click on the folder Display Templates and then Search.     Blog3
  5. Under the search folder you will find html and .Js files present.
  6. We are more intereste d in ‘Control_SearchResults.html’. Make sure you click in html file not JS file with same name.

If you are not familiar with this structure then you must be thinking that “Why JS and HTML files with two names?”

In brief, actually display templates are JS files only but just to the sake of simplicity MS has provided HTML so that it is easier to modify/edit display templates. There is out of box event receiver attached to this list when there is any modification detected in HTML file, a new version of JS will be generated by the event receiver. Display Templates are actually split into two different files, the Control and the Item.

Control Display Template

This is the part of your design that does not get repeated as each items gets loaded in the Search Results. It’s the container around and where you refer to any custom CSS or JS files you may want to use.

Put simply, it’s the box where search results will be loaded.

Item Display Template

This is where you design how each item will look like and which managed properties from the result item will be used and where on the design. If I take my example above, that’s all in the Item Display Template. The size of the rectangle, where the Title goes and where the Description goes as well as the animation itself.

Search Results web part of SharePoint uses ‘Control_SearchResults.html’ display template which is available under Master Page Library –> Display Templates –> Search.

Now lets start actual modification of the template:

  1. Download a copy of ‘Control_SearchResults.html’. Click Control_SearchResults.html template file and Download a Copyblog5
  2. Rename the downloaded file as Control_SearchResults_Custom.html. Open the control template file in any of your preferred editors such as SharePoint Designer and customize the file, as per our need.blog6
  3. Change the Title in this HTML file, this title will be available for selection in web part properties.
  4. Now we want to get the search text entered in to search box web part, for that we will use ctx object as following ctx.DataProvider.get_currentQueryState().k
  5. Search if(ctx.ClientControl.get_shouldShowNoResultMessage()){    line in HTML file (as shown in below image).
    Search_Result_SP_2013_Display_Template_1
  6. Find the unordered list just below that code and modify it according to your need.
  7. For example if you don’t want to show any message until user enters some keyword, replace if statement line with
    var currentQueryTerm = ctx.DataProvider.get_currentQueryState().k; if(ctx.ClientControl.get_shouldShowNoResultMessage() && currentQueryTerm!=””){
    (As shown in below image)
    Search_Result_SP_2013_Display_Template_2
  8. Save the modified HTML file.
  9. Go to Master Page Library –> Display Templates –> Search and upload new control template.
  10. You will see that respective ‘Control_SearchResults_custom.js’ will be created by SharePoint.
  11. Now go to your page where Search Result web part is added, edit Search results web part. Under Display Templates section select newly uploaded display template.( as shown in below image).Select our custom control display template.

  12. Save the changes and that’s it, you will see the  expected result.

Hope this information is helpful for you to modify display templates.

Happy SharePointing 🙂

 

Not able to view result while building Search Query


Hello SharePointers ;),

Here is another quick finding which I just thought should be shared to save someone precious time. If you are dealing with SharePoint 2013 search and Result sources, you must try to test the query template before saving it. If you are not able to see any results instead just a red error saying below text don’t get scared I have quick solution for you.

“The Search display templates are not present on this site collection. To add them, you need to activate the “Search Server Web Parts and Templates” feature on the Site Collection Features page.

Display Error: The display template had an error. You can correct it by fixing the template or by changing the display template used in either the Web Part properties or Result Types.

Template ‘~sitecollection/_Catalogs/masterpage/DisplayTemplates/System/Control_QueryBuilderPreview.js’ not found or has sytax errors. (Load Template: ) ”

Search_Error

You must get frustrated because your search is properly configured, its crawling the data and you are able to see results while searching.  After looking at what was missing I found that in fact there was a feature missing on the Central Administration site collection. The feature is called Search WebParts.

But its not so quick that you just navigate to Site collection feature and activate it. As this feature is present at central admin site collection level so it requires extra privileges to activate it. To fix the problem, simply run the following command in an elevated SharePoint Management Shell:

Enable-SPFeature SearchWebParts -url http://<central admin url>

Bingo!! Just, re-open the “build your query” page, and everything should work properly!

Search_Error

Hope that post was able to help you. Please press like button it that saved your time.

Happy SharePointing 🙂

SharePoint : Custom Task Email with Outlook ribbon control “Open this Task”


Hi Guys,

It was really interesting to work with my latest client. They had an very interesting and innovated idea to automate their manual file approval process.  The requirement was to create a multistage state machine workflow where users have privilege to approve and reject documents with some comments. It was very interesting to work with end client(as they them self don’t know what they actually want).

I will not go deep into the workflow but surly will come back with another post explaining it. This post is more about a requirement which I faced during the development of the workflow. Just wanted to share the design of the Workflow:

WF

Requirement:  As a user, I should have receive an email from workflow with custom subject having document name embedded to the subject where I can directly do some action like approve/reject or comment on the document. I should see a button in email “Open this task” which will redirect me to the page where I can take actions.

1

It looks bit easy but have hidden challenges in it. Most us are aware of the fact that when a task item is created, either directly in the task list or through a Workflow an alert email would be sent saying a task has been assigned to you (provided alerts are enabled). When we look at that email in Outlook we see an additional control in the ribbon “Open this Task” under the group “Open” as shown in above figure. Let me rephrase the requirements:

  1. We have to customize the body and subject as per their request
  2. Ribbon button “Open this Task” should present when the email is sent

The above mentioned two points are two sides of the coin, which means if you are able to see one then second will not be visible to you. So the final solution’s must have some trick to achieve both cases.

Solution: 

One point is clear that we can not go along with the default workflow email feature, we have to use “SmtpClient” or “SPUtility” to create email with custom body and subjects. Now question is how one can get ribbon button using SPUtility send email function.

  • I have used “SPWorkflowTaskProperties” class in the Task Created Event and sent it two properties “SPWorkflowTaskProperties.HasCustomEmailBody = true” and “SPWorkflowTaskProperties.EmailBody= <My Custom HTML Email Body> ” With this I can satisfy requirement 1 & 2 but not 3 (listed above).  Also if I were to create a task and if I want to send an email using Event Handler on item created I may not be able to use this. So for the generic purposes this would not fit in.
  • Next I thought of modifying the alert template but then it would have an impact on all task lists which is not a recommended option.
  • So, I have decided that I have to disable the alerts for that Task list and use “SmtpClient or SPUtility” class instead to send email which can be used at different requirements such as “event handlers/workflows or any other for that matter. With this class we do have control over all the aspects “From, To, Subject, Email Body, etc”. The only problem with this to achieve the requirement # 2 (Ribbon control in outlook). I always wondered how would outlook recognize that an email is for Task, how is this ribbon control activated as soon as it sees an email.

I believed that the alert email sent has some headers which mark this as Task which is understood by outlook and displays the controls accordingly. So now the question is what are those headers. After some research I figured out the Mail message headers. Below is the code for sending an email which also includes those headers and satisfies all the above mentioned requirements (1 & 2)

/// <summary>
/// Method used to send the task created email instead of default SP WF email notification
/// </summary>
/// <param name=”web”></param>
/// <param name=”htmlBody”></param>
/// <param name=”ToEmailId”></param>
/// <param name=”listItem”></param>
/// <param name=”emailSubject”></param>
/// <returns></returns>
public bool SendTaskMail(SPWeb web, string htmlBody, string toEmailId, SPList taskList, string itemId, string emailSubject)
{
try
{
string domain = web.Site.WebApplication.OutboundMailSenderAddress.Remove(0, web.Site.WebApplication.OutboundMailSenderAddress.LastIndexOf(‘@’));
var messageHeaders = new StringDictionary();
messageHeaders.Add(“to”, toEmailId);//author.User.Email);
messageHeaders.Add(“subject”, emailSubject);
messageHeaders.Add(“content-type”, “text/html”);

messageHeaders.Add(“Message-Id”, “<3BD50098E401463AA228377848493927” + Guid.NewGuid().ToString(“D”) + domain + “>”);
messageHeaders.Add(“X-Sharing-Title”, ConvertToBase64String(“Body”));
messageHeaders.Add(“X-AlertTitle”, ConvertToBase64String(“System”));
messageHeaders.Add(“Content-Class”, “MSWorkflowTask”);
messageHeaders.Add(“X-AlertWebUrl”, ConvertToBase64String(web.Url));
messageHeaders.Add(“X-AlertServerType”, “STS”);
messageHeaders.Add(“X-AlertWebSoap”, ConvertToBase64String(web.Url + “/_vti_bin/alerts.asmx”));
messageHeaders.Add(“X-Sharing-Config-Url”, “stssync://sts/?ver=1.1&type=tasks&cmd=add-folder&base-url=” + Uri.EscapeDataString(web.Url) + “&list-url=” + Uri.EscapeDataString(taskList.RootFolder.ServerRelativeUrl) + “&guid=” + Uri.EscapeDataString(taskList.ID.ToString(“D”)));
messageHeaders.Add(“X-Sharing-Remote-Uid”, taskList.ID.ToString(“D”));
messageHeaders.Add(“X-Sharing-WssBaseUrl”, ConvertToBase64String(web.Url));
messageHeaders.Add(“X-Sharing-ItemId”, ConvertToBase64String(itemId));

SPUtility.SendEmail(web, messageHeaders, htmlBody);

return true;
}
catch (Exception ex)
{
Utilities.UpdateLogErr(ex, “Error while sending email. Please find details in audit logs”);
return false;
}
}

Hope this helps!

Thank you for your time.

Happy SharePointing 🙂 !!

Upload a file in SharePoint using REST and c# console application


Hi,

In my last post, I have explained how to create a sub site using REST from and Console application. In this blog  will explain how we can upload a file, which is located in file system, will be uploaded to sharepoint Document library. So if I brief about requirement here:

  • Need to upload a file to already created SharePoint site.
  • Use REST(not even CSOM) will be used in order to create.
  • API call will be send from some other machine(not SharePoint machine) which is in same domain and can access the SharePoint site.

Follow the given steps to achieve the same:

1) Create a simple console application with c# selected language.

1

2) Next is to install the Newtonsoft.Json. Navigate to Visual studio menu: “View” > “Other Windows” > “Package Manager Console” and Then type the following: “Install-Package Newtonsoft.Json”

2

3) Now install RestSharp using the package manager. Type the following: “Install-Package RestSharp”

3

4) Now open the Program.cs file of console application and paste the code below:

string siteurl = “http://sharepoitsite/sites/subsite&#8221;; //site on which file needs to be uploaded (don’t put / at end)
string documentlibrary = “Documents”; //Document library where file needs to be uploaded

string filePath = @”C:\Users\mohit.vashishtha\Desktop\test.html”;

byte[] binary = System.IO.File.ReadAllBytes(filePath);
string fname = System.IO.Path.GetFileName(filePath);
string result = string.Empty;
//Url to upload file
string resourceUrl = string.Format(“{0}/_api”,siteurl);

RestClient RC = new RestClient(resourceUrl);
NetworkCredential NCredential = System.Net.CredentialCache.DefaultNetworkCredentials;
RC.Authenticator = new NtlmAuthenticator(NCredential);

Console.WriteLine(“Creating Rest Request”);
RestRequest Request = new RestRequest(“contextinfo?$select=FormDigestValue”, Method.POST);
Request.AddHeader(“Accept”, “application/json;odata=verbose”);
Request.AddHeader(“Body”, “”);

string ReturnedStr = RC.Execute(Request).Content;
int StartPos = ReturnedStr.IndexOf(“FormDigestValue”) + 18;
int length = ReturnedStr.IndexOf(@”””,”, StartPos) – StartPos;
string FormDigestValue = ReturnedStr.Substring(StartPos, length);

Console.WriteLine(“Uploading file Site……”);

resourceUrl = string.Format(“/web/GetFolderByServerRelativeUrl(‘{0}’)/Files/add(url='{1}’,overwrite=true)”, documentlibrary ,fname);
Request = new RestRequest(resourceUrl, Method.POST);
Request.RequestFormat = DataFormat.Json;
Request.AddHeader(“Accept”, “application/json;odata=verbose”);
Request.AddHeader(“X-RequestDigest”, FormDigestValue);
Console.WriteLine(“File is successfully uploaded to sharepoint site.”);
Console.ReadLine();

5) Now application is ready to run. Make sure that all the namespace issues are resolved.

6) It will take some time to create site but output window will display the details it got from the Request.

In case of already existing site:

54

Hope this information is helpful to you. Thanks for time.

Happy SharePointing 🙂

 

Get SharePoint search results using REST ajax call from Html


Hi,

We all aware the SharePoint exposed REST Api in order to perform various action at client side. There are some other ways also which a developer can adopt instead of REST(like CSOM) but that requires again client context. So it is recommenced that if we are using SharePoint Apps then use CSOM to get and update the data. Where as REST can be used in apps as well as some external applications.

This blog is intended to show how user can utilize the REST api and get the SharePoint Search results. There are few pre-requisites or conditions which must be fulfilled before proceeding further.

  • The machine where this code will be executed must have access to sharepoint site which means machine must be either in same domain or cross domain accessibility is there.
  • Search must be configured at SharePoint server.
  • Data must be properly crawled and you must get some results when searched for some keyword.

Steps:

1) Open the Explorer or any desired location and create a text file with ant desired name.

2) Now open this txt file in Notepad and click on “Save as..” option. Here select the “Save as type” as “All” and save the file in “.html” format as shown below:

1

3) Now open this file in any desired text editor(I love to use Notepad++ for same) and paste the following code:

<script src=”http://code.jquery.com/jquery-2.1.4.js&#8221; type=”text/javascript”></script>
<script type=”text/javascript”>
$(document).ready(function () {
$(“#SearchQuery”).click(function() {
$.ajax({
url: “http://<server URL>/_api/search/query?querytext='”+$(“#search-input”).val()+”‘&sourceid=%27b09a7990-05ea-4af9-81ef-edfab16c4e31%27″,
headers: { Accept: “application/json;odata=verbose” },
success: function (data) {

console.log(data.d);

},
error: function (jQxhr, errorCode, errorThrown) {
console.log(errorThrown);
}
});
});

});</script>
<input type=”text” id=”search-input”>
<input type=”button” id=”SearchQuery” value=”Search”>
<div id=”resultsDiv”></div>

Issues faced:

1) minFreeMemoryPercentageToActivateService memory issue:

2

Resolution: Open the web config of the web application and find out the entry with

<serviceHostingEnvironment aspNetCompatibilityEnabled=”true” />

to

<serviceHostingEnvironment aspNetCompatibilityEnabled=”true” minFreeMemoryPercentageToActivateService=”1″/>

Happy SharePointing 🙂

 

 

 

Create Sub Site in SharePoint using REST and c# console application


Hi,

Recently, I got an requirement to create a SharePoint sub site from the some .Net application which is hosted somewhere hosted outside the SharePoint environment. It is provided that the .Net application can be added in same domain. So if I brief about requirement here:

  • Need to create a sub site in SharePoint site collection based on some default template(later on custom template)
  • Use REST(not even CSOM) will be used in order to create.
  • API call will be send from some other machine(not SharePoint machine) which is in same domain and can access the SharePoint site.

There are few challenges which I faces if we try to work with such kind of requirements. The authentication tokens and RequestDigest data are the crucial things to retrieve.

I have explored some way to execute REST API and create a sub site without navigating to SharePoint site. Follow the given steps to achieve the same:

1) Create a simple console application with c# selected language.

1

2) Next is to install the Newtonsoft.Json. Navigate to Visual studio menu: “View” > “Other Windows” > “Package Manager Console” and Then type the following: “Install-Package Newtonsoft.Json”

2

 

3) Now install RestSharp using the package manager. Type the following: “Install-Package RestSharp”

3

4) Now open the Program.cs file of console application and paste the code below:

RestClient RC = new RestClient(“http:/SharepointSiteUrl/_api”);
NetworkCredential NCredential = System.Net.CredentialCache.DefaultNetworkCredentials;//new NetworkCredential(“mohitvashishtha”, “******”, “mydomain”);
RC.Authenticator = new NtlmAuthenticator(NCredential);

Console.WriteLine(“Creating Rest Request”);

RestRequest Request = new RestRequest(“contextinfo?$select=FormDigestValue”, Method.POST);
Request.AddHeader(“Accept”, “application/json;odata=verbose”);
Request.AddHeader(“Body”, “”);

string ReturnedStr = RC.Execute(Request).Content;
int StartPos = ReturnedStr.IndexOf(“FormDigestValue”) + 18;
int length = ReturnedStr.IndexOf(@”””,”, StartPos) – StartPos;
string FormDigestValue = ReturnedStr.Substring(StartPos, length);

Console.WriteLine(“Creating Site……”);

var Data = string.Concat(
“{‘parameters’:{‘__metadata’:{‘type’:’SP.WebCreationInformation’},”,
“‘Title’:’Team projects 3′,’Url’:’TeamProjects3′,’WebTemplate’:’STS’,”,
“‘UseSamePermissionsAsParentSite’: true}}”);

Request = new RestRequest(“web/webs/add”, Method.POST);
Request.RequestFormat = DataFormat.Json;
Request.AddHeader(“Accept”, “application/json;odata=verbose”);
Request.AddHeader(“X-RequestDigest”, FormDigestValue);
Request.AddParameter(“application/json;odata=verbose”, Data, ParameterType.RequestBody);

Console.WriteLine(RC.Execute(Request).Content);
Console.WriteLine(“SharePoint Site is successfully created.”);
Console.ReadLine();

5) Now application is ready to run. Make sure that all the namespace issues are resolved.

6) It will take some time to create site but output window will display the details it got from the Request.

In case of already existing site:

4

 

Just a little explanation about the above code, this code is actually getting the request digest first and then in second REST request it will pass the same reader digest. There are few chances that this code will throw 401 unauthorized error if user does not have permission on sharepoint site. But there are option to hardcode the credential too using NetworkCredentials object commented in above code.

Hope this information is helpful to you. Thanks for time.

Happy SharePointing 🙂