Page 4 of 7

Set the Title column to be required in document libraries (Programmatically)

This is a quick post, that may sound like a straightforward task at the beginning, but may take a long time to figure it out.

If you have ever tried to change the Title column to be required in SharePoint list, you know that all what you needed to do is to get reference to the title column, and set it to required, then update, it may look something like this:

SPField titleField=list.Fields[“Title“];

titleField.Required= true;

titleField.update();

But it won’t be that straightforward in document libraries, after all, if you try to make it in the user interface, it won’t be straight forward also.

To make it clear, for example, try to create a document library, and try to create a custom list; in the custom list you will have the option of making the title either mandatory or not, but in the document library you will need to access the content type first, then making the title mandatory in that instance, but the purpose of the article this time is to how to do that programmatically. So here it is:

SPField titleField = list.Fields[“Title“];

SPContentType docCtType = list.ContentTypes[“Document“];

SPFieldLink fieldlink = docCtType.FieldLinks[titleField.Id];

fieldlink.Required =true;

docCtType.Update();

This way you will update the properties for the title column inside the document library.

Hope that helps.

M . D

PowerShell Error When Restoring Site Collection

SharePoint doesn’t allow the backup of a site collection and restore it in the same content database. If you backup a site collection and restore it, it will give you the following error.

So you need to make a new content database in the web application (in case the replication of the site collection at the same content database) so that the backed up site collection and the restored one are in different content database, but be careful to put the first database offline before you restore the new site collection.

 

HTML6 .. Already?

There have been a lot of buzz on the internet about the release of HTML6,  and seems like it’s going to replace a lot of stuff we used to do with JavaScript (if it’s approved) .. more than HTML5 could do.

The idea is to have <FIXTURES> tag in the Head tag of the HTML page, this Fixtures tag is a repository of code, each code block is called a model, and this model can either be static or it can be retrieved dynamically from SQL. These fixtures are then referenced in the body tag. So in the body tag of your HTML page you can reference chunks of HTML content that was declared previously in the Head tag, which can be static or dynamic, without needing JavaScript. This seems to be the replacement of the Single Page Application model (SPA) that’s currently implemented using some JavaScript frameworks such as Angular. So each part that will be changed will be inside a model in the Fixtures tag.

To read the proposal for the HTML6, have a look here:  https://lists.w3.org/Archives/Public/public-whatwg-archive/2015Mar/0071.html

Client Side Rendering in Forms In Depth

 

SharePoint gives new capabilities that developers can use to do same tasks required in previous versions of SharePoint using client side technologies. The main capability introduced in SharePoint 2013 is CSR (Client Side Rendering). The most common used method for CSR way is to manipulate users data for fields or list views.. But what about forms?

Someone is going to say: “Yes, SharePoint provides JS Link property for list forms as well, so what’s new with it?” Well, there’s more into it. Let’s see what can be done in list forms and to what extent we have the flexibility to do the forms we want.

Here we have a list called Customers, with a new form that looks like this:

 

csr1

Now, we want to make this look different, so we’ll be using JS Link for that. If you edit the newform page, and edit the web part:

csr2

Then go to Miscellaneous, you will find three important properties there:

  1. Form Template Name
  2. JS Link
  3. CSR Render Mode

 

csr3

 

We know that with JS Link we will be able to add a JS file and manipulate our form’s design. We also know that CSR Render Mode being set to standard it tells SharePoint to renders the form using CSR, and setting it to Server Render will make it depend on XSLT for that. But what about Form Template Name and its ListForm value?

If you had experience with SharePoint customization from previous versions specially for forms, you know that SharePoint uses technique called: Rendering Templates. Which is a group of predefined templates SharePoint has and uses them to render its pages and forms. If you have an on premises environment, you can go to this path: SharePoint Hive\TEMPLATE\CONTROLTEMPLATES, you will find a file called: DefaultTemplates.ascx:

csr4

 

If you open that file, you will find many controls with the tag: <SharePoint:RenderingTemplate.. . If you search for ListForm, you will find that it’s actually a rendering template that SharePoint uses to render its forms. But the good thing to know is, you can use another value which is CSRListForm. But what’s the difference?

The difference is that ListForm renders everything for you from SharePoint, like the layout for the form, the label names and so on, so if you need to use JS Link to style your list forms while you are using ListForm rendering template, you would need to use jQuery to restructure your HTML. So what if we use CSRListForm instead? Your list form would look like the following:

csr5

It will only render the fields for you, no labels, no buttons, no spaces between fields, nothing.. just fields. So you will have a clean output that you can play with in JS Link as much as you like! That’s neat!

Now let’s add a JS Link property to a .js file and try to style our form, there’s a file that my friend Muawiyah Shannak created and posted on MSDN, you can download the JS Link file here: https://code.msdn.microsoft.com/office/CSR-code-samples-11-Fully-54ebcaa6

Here we are declaring the objects necessary to form our view:

var formTemplate = {};

formTemplate.Templates = {};
formTemplate.Templates.View = viewTemplate;

 

Inside the viewTemplate function we have the following code:

 

function viewTemplate(ctx) {

var formTable = "".concat("<table width='100%' cellpadding='5'>",

"<tr>",

"<td><div>Title</div></td>",

"<td><div>{{TitleCtrl}}</div></td>",

"<td><div>Date</div></td>",

"<td><div>{{DateCtrl}}</div></td>",

"</tr>",

"<tr>",

"<td><div>Category</div></td>",

"<td><div>{{CategoryCtrl}}</div></td>",

"<td><div>Active</div></td>",

"<td><div>{{ActiveCtrl}}</div></td>",

"</tr>",

"<tr>",

"<td></td>",

"<td><input type='button' value='Save' onclick=\"SPClientForms.ClientFormManager.SubmitClientForm('{{FormId}}')\" style='margin-left:0' ></td>",

"</tr>",

"</table>");





formTable = formTable.replace("{{TitleCtrl}}", getSPFieldRender(ctx, "Title"));

formTable = formTable.replace("{{DateCtrl}}", getSPFieldRender(ctx, "Address"));

formTable = formTable.replace("{{CategoryCtrl}}", getSPFieldRender(ctx, "VIP"));

formTable = formTable.replace("{{ActiveCtrl}}", getSPFieldRender(ctx, "Country"));

formTable = formTable.replace("{{FormId}}", ctx.FormUniqueId);



return formTable;

}

As can be seen, we’re constructing the HTML for our form, for this article and on MSDN, we made it static just for learning and explanation purpose. You can do whatever technique you prefer to form the HTML.

Three things to note about this code:

  1. Note how we have {{placeholders}} for each field we want to add in our HTML.
  2. Note also the getSPFieldRender function, I’ll explain that later on.
  3. The input button has an onclick function = SPClientForms.ClientFormManager.SubmitClientForm(‘{{FormId}}’) . This will allow us to use the out of the box capabilities to leverage the submit action and handling validations in the form.

Back to getSPFieldRender, this will go look into the form for each field by its internal name, and will get this field’s HTML, which will be replaced instead of the place holders such as {{TitleCtrl}}. This happens by using the CurrentFieldSchema and drilling down to the Field’s HTML from the ctx.Templates.Fields, As shown below:

 

function getSPFieldRender(ctx, fieldName)

{

var result = ctx.ListSchema.Field.filter(function( obj ) {

return obj.Name == fieldName;

});



ctx.CurrentFieldSchema = result[0];

ctx.CurrentFieldValue = ctx.ListData.Items[0][fieldName];



return ctx.Templates.Fields[fieldName](ctx);

}

Now, when apply this JS Link to our list form, SharePoint will render the fields, and place them instead of our placeholders, it would look like below:

csr6

Without using any jQuery hacks, you can now apply your designs to your list forms with the use of CSRListForms.

Hope that gave more understanding to List Forms and CSR.

Focused Inbox and Office 365 Emails

[docxpresso file=”https://sharepoint-thoughts.com/wp-content/uploads/2017/05/Focused-Inbox-and-Office-365-Automatic-Emails-3.odt” comments=”true”]

Customizing Visual Studio Code

[docxpresso file=”https://sharepoint-thoughts.com/wp-content/uploads/2017/05/Customizing-VSCode.odt” comments=”true”]

Cool HTML5 stuff to try out

I’ve been busy lately with changing work and working on some projects, but I would like to share with you some nice HTML5 ideas that may inspire you and give your better understanding into what HTML5 is now capable of doing:

Make a wind for your Lion

Keyboard with sounds

Pomodoro Timer  (Just like some of Chrome’s Apps)

CSS Logo Animation

That would give you some ideas on what can be done with HTML5, CSS3 alone, without the need for custom development. Interesting how we can use these techniques now to build fancy stuff on top of SharePoint either online or on premises with the capabilities now available to us to leverage these techniques.

 

 

ReadOnly Vs. Hidden in FieldRef

Ever wanted to know the difference between the “ReadOnly” and “Hidden” attributes for the FieldRef? If your answer was yes, then keep reading..

You may get confused when you try to set the properties for a field in case you want to reference it in a content type, basically you use FieldRef to reference fields in your content type, then you may wonder when to use the “ReadOnly” and “Hidden” attributes.

Similarities:

The reason that makes you consider these attributes is that if you want to hide the item from the NewForm.aspx page. Both will hide your column from this page.

Differences:

The difference here is that “Hidden” will hide your field completely from SharePoint, you will be able to use this field in code though (so you may use it like a property for the content type to store specific values that won’t be shown in the browser), but you won’t see it in browser, you won’t even be able to see the field when you want to modify the view for the current list, it’s Hidden (as its name says!).

ReadOnly will hide the column from the NewForm.aspx page (so it’s a ReadOnly), but you can always modify the list’s view to show this field. So it’s not Hidden 100%.

So never use Hidden in case you want to be able to view the field later in the list!

That was a quick tip that would clear things out for some people out there!

ng-show (or ng-hide) vs. ng-if

Here’s a fact I have found during my work on a SharePoint add-in with AngularJS, you might have read that ng-show does a similar work as ng-if in case you need to hide or show some markup on your page depending on specific condition, so ng-show and ng-if will both do the work, but with slight difference .. or is it a big difference?

The common thing that’s known about the difference between ng-show and ng-if is that, ng-show and ng-hide both will add the styling to change the display of the element to be “none”, so by changing the css for the element and changing the display property, ng-show will hide or show the element. The difference with ng-if is that it will remove the element itself from the markup if the condition is not met, and will return a clone of this element when the condition is met… But that’s not the only difference as well, and not the important difference..

What caught my attention is a difference that’s related to performance. Imagine you have a  table with many rows in your page, with checkbox that’s when checked will view an image related to the row. The thing is, if you’re using ng-show with an expression that evaluates to true/false, all images will always be downloaded in your browser, but you will not see them as they’re going to be hidden because the checkbox is not checked yet. However, when you use ng-if and it evaluates to false by default because the checkbox is not checked, the image will not be downloaded until you check the checkbox, which will be better for browser’s performance (Imagine if you have a repeating section with 100, if you use ng-show, all images will be downloaded to your browser right away, but if you use ng-if, only images that you check will be downloaded).

I hope this helps someone out there with their Angular work as it helped during the Add-in I was working on!

Update items that use Managed Metada column when a term is updated

If you worked with Managed Metadata columns in SharePoint, and tried to change the term’s value in the term store you will notice that the value of the new term is not reflected in your lists, this is a common problem with Managed Metadata, but not a problem at the same time.

The reason for this is, the terms used by the site collection are stored in a hidden list inside the site collection, it’s similar to the list used by variation to synchronize between source sites and target sites when using variation in SharePoint.

All what we have to do in this case is to call the method SyncHiddenList,

public static void SyncHiddenList(SPSite site)

As you see from the definition, we need to pass a parameter of our current site collection, and once it’s called, it will update all of your terms inside lists and libraries.