Some thoughts on programming stuff

Tag: JavaScript

Jump to current date cell on Open in Google Sheets

If you are like me and usually updates manually a spreadsheet to track your progress in a daily basis related to something like diet or spenditures, you will know that your spreadsheet may become very long. That’s why I created an small script to allow me jump to current date cell on Open in Google Sheets automatically.

The script will run like a macro and after a few moments you have opened the spreadsheet, it will focus the correct cell!

Continue reading

SharePoint breadcrumb for better navigation

This blog post will talk about an idea of customization of the SharePoint breadcrumb for better navigation. The solution uses the out-of-box Delta Breadcrumb Dropdown component to provide the data required to render the breadcrumb using pure JavaScript for that.

Important notes:

  • This solutions works only for Classic Experiences;
  • I would recommend enabling only to internal pages branded using the System Master Page;
  • The JavaScript needs to be installed using a Custom Action or calling the JS file from the Master Page;
  • This solution may not be a good practice for SharePoint Online environments, if you are focused on working only with Modern experiences.
Continue reading

Get current page layout with JavaScript in SharePoint

Today I’m bringing an useful code snippet to get current page layout with JavaScript in SharePoint. It helped me on finding out which Page Layout I was running my JavaScript code. Here you can find a pure vanilla JavaScript example that builds the URL for the SharePoint REST API. First it requests only the PublishingPageLayout field of the current page. Then it logs the information in the console, but I suggest you to just do that for testing purposes.

Page layouts are used in SharePoint to customize and define where the content will be displayed in your Web Page. Essentially, it’s a template for your Web Page that helps you on building a better experience. It benefits those who access your content and to help the content creators when they need to create a new page for you Site or Intranet.

Page layouts must be attached to a Content Type, so you can reference site columns and fields inside the template to let SharePoint save and present the data in the page library just like any document library you had been working with. Also it let’s you define different ways to present the fields, for example the Title or the Page Content, when in the Display Mode and Edit Mode.

This script can be particularly useful when you need to accomplish some specific business logic that requires to run a JavaScript function in an specific layout and you can’t or don’t want to make changes to the page layouts directly. I say that because, due the fact that Page Layouts are just like templates, you can add Script tags directly to the Page Layouts and call only the JavaScript files you /want when a user selects that Page Layout during the content creation process.

Image result for sharepoint page layout
A bunch of Page Layouts – dotnetcurry
Continue reading

File Constructor support in Internet Explorer

As you may know, the File constructor support in Internet Explorer doesn’t exist. For security reasons, you may not be able to abstract the File object as you would like to do normally. It doesn’t if other Libraries that use the File API to upload files like the ng2-file-upload require it, it won’t work.

According to MDN Docs, the File interface provides information about files and allows JavaScript in a web page to access their content. A File object is a specific kind of a Blob, and can be used in any context that a Blob can. In particular, FileReaderURL.createObjectURL()createImageBitmap(), and XMLHttpRequest.send() accept both Blobs and Files.

Since we needed to create Text Files from a TextArea input in a browser and IE didn’t allowed me to do that, after finding the solution for that I’ve decided to share with you what I’ve done to solve the problem in our project while using vanilla JavaScript.

Image result for file
A bunch of files (Findel International)
Continue reading

Get web URL from document URL in SharePoint

Maybe you need to get web URL from document URL in SharePoint, or even just grab the Site Collection URL and do some awesome logic and update some data in your site. But you are aware that it can be costy and require some array splits, substrings and those kind of tricks. So here is a code snippet to save your time to enjoy a good cup of coffee with your colleagues!

The document URL usually is composed by a site collection part, the specific web part, the Library name and then the final part containing the folders and the file name. The script basically will perform a request to a SharePoint API. Our parameter is the folder or library URL (cutting the filename) that will be made automatically. Finally, the result will come with the Web and Site Collection urls for that context.

Get web URL from document URL in SharePoint - A bunch of files in folders inside drawers (SharePoint)
A bunch of files in folders inside drawers (SharePoint) (Source: Howtogeek)
Continue reading

Copy and paste images in SharePoint

This blog post covers a customized script that can be applied to SharePoint sites, letting you be able to copy and paste images in SharePoint. The content is focused on developers, because the installation process may not be so easy to be executed.

Copying and pasting images is a common customer demand to make their lifes easier when working with SharePoint pages and publishing new content. Although my automatic answer has been always “Just upload it through the ribbon, so you can add metadata and ensure content quality and reusability“, I must admit that this way isn’t THAT user-friendly.

That’s why I decided to develop some JavaScript code to add this functionality to my Web Pages. So the users would be able to add their images faster. Basically, I’ve created a GitHub repository to keep the code, so maybe someone can help me on refactoring it or adding support for others browsers.

The project to enable copy and paste images

Github project: https://github.com/wiliammbr/sp-copypasteimages

The project is a SharePoint provider-hosted app that publishes some JavaScript files to your Site Assets library. It was developed based on some SharePoint projects in the PnP – Patterns and Practices repository. Microsoft and the community maintains the repository updated. Also, it will add ScriptLinks to insert those script files into your pages. In my example, the app was hosted in an Azure Cloud Service.

But if your prefer, it’s possible to add those scripts into your Master Page files too, so you don’t need the Provider-hosted App approach.

Continue reading

Get user info programmatically in SharePoint

Sometimes when we are developing a custom solution, we need to get user info programmatically in SharePoint. It doesn’t matter if you need to show a special message to an specific user using its Personal Name or maybe just fill a SPFieldUser column in a specific list, you’ll probably need one of those code snippets to do the trick.

I’m going to split it into Server-side object model for C# developers, REST API (for SharePoint 2013 projects or newer) and the old-fashioned JavaScript object model introduced in SharePoint 2010. Each one wil let you capture the current user or get an user by its ID or login name. After that it’s up to you what you are going to do with the data.

SharePoint users inside the User Information List (hidden)
Continue reading

Hide Content Query Web Part when empty

For those who want to hide Content Query Web Part when empty, in other words, when it returns not data, this post is for you! The solution won’t require you to dive into XSL world, it’s pure JavaScript. Basically you will drop the code code snippet to your pages and everything will work.

The Content Query Web Part (CQWP) is a great way to query data from other sites and present it inside one page. Content Query Web Part can be used with their default layouts or you can create some if want.

According to a Code Project article: ” This web part is rather nifty and it has many cool features. With the CQWP, you can now control how the content is displayed. We can use html tables to format the display however we wish. This article explains how to customize the CQWP using SharePoint 2010 without any code and deployment. “.

The Content Query Web Part - Hide Content Query Web Part when empty
The Content Query Web Part

Unfortunatelly, this post may be a little bit outdated because Microsoft reinforces that Content Search Web Part is a better option now. You can check this post when to use the Content Query Web Part or the Content Search Web Part in SharePoint.

Continue reading

© 2024 wiliammbr's blog

Theme by Anders NorenUp ↑