Calculated field

If you are defining a SharePoint list using declarative definitions in Visual Studio, you won’t find calculated field in the field types. I am using two examples, the first one is with date and the other one is with integer.

Date data type:

The solution will be create your field with any data type and go to the schema file and manually do the following changes:

  <Field Type="Calculated" DisplayName="ValidationDays" EnforceUniqueValues="FALSE" Indexed="FALSE" 
             Format="DateOnly" LCID="1033" ResultType="Text" ReadOnly="TRUE"
             ID="{776ce876-209f-4063-adb8-0829572049a2}" 
             StaticName="ValidationDays" Name="ValidationDays">
        <Formula>=StartDate-StopDate</Formula>
        <FormulaDisplayNames>=StartDate-StopDate</FormulaDisplayNames>
        <FieldRefs>
          <FieldRef Name="StopDate" />
          <FieldRef Name="StartDate" />
        </FieldRefs>
      </Field>
  • Type –> Calculated
  • ReadOnly –> True
  • Add the formula <Formula>=StartDate-StopDate</Formula>
  • Add the field ref

<FieldRefs>

<FieldRef Name=”StopDate” />

<FieldRef Name=”StartDate” />

</FieldRefs>

Deploy the feature to the SharePoint server and activate the feature.

Test the list.

Integer data type:

Repeat the same steps and the field definition in the schema field will be:

<Field Name="FreeTickets" ID="{90dfb1f0-257e-4530-b885-84842594323e}" Type="Calculated" >
				<Formula>= Tickets - SoldTickets</Formula>
<FieldRefs>
          <FieldRef Name="Tickets" />
          <FieldRef Name="SoldTickets" />
        </FieldRefs>
			</Field>

Deploy the feature to the SharePoint server and activate the feature.

Test the list.

Error Occurred In Deployment Step ‘Recycle IIS Application Pool’ – Invalid Namespace SharePoint

One of the common deployment errors is Error Occurred In Deployment Step ‘Recycle IIS Application Pool’ – Invalid Namespace SharePoint. It occurs if the developers are trying to deploy SharePoint solution to SharePoint 2019 on premises.

The solution: you need to install IIS 6 management backward compatibility.

The steps (windows server):

  1. serer manager
  2. Click on “Add Roles and Feature”
  3. Click next in the appeared dialog until you reach “Server Roles”.
  4. expand “Web Server (IIS)” and then expand “Management Tools”
  5. from  “IIS 6 Management Compatibility” select “IIS 6 WMI Compatibility”, “IIS 6 Metabase Compatibility” & “IIS 6 Management Console” and click Next.
  6. Click install

Data Migration

Imagine the following scenario, in your company there is a SharePoint 2013 web application with huge document library and unique permission levels. The organization revamped this web application to SharePoint 2019 and now they need to migrate the documents along with the security.

Steps to follow for such scenario will be:

  1. Export the document library with the security by using the following CMDLET
    • Export-SPWeb -Identity <Source Site URL> -Path “C:\BKP\docbak.cmp” [-ItemUrl <Library URL>] [-IncludeUserSecurity] [-IncludeVersions] [-NoFileCompression]
  2. Import the exported library by using the following CMDLET
    • Import-SPWeb -Identity <Distination URL> -Path “C:\BKP\docbak.cmp” -Force -NoFileCompression -IncludeUserSecurity

Notice file compression is not used because if the document library is huge, the command could take days to run.

Sometimes the import command will fail. If SharePoint give reasons for failure and mostly it will be related to the security issues, you fix them and try again. If the failure continue to happen you can try the old cmd tool for SharePoint stsadm.exe

stsadm –o gl-importlist -url <Distination URL> -filename “C:\BKP\docbak.cmp” -includeusersecurity -nofilecompression

References:

  1. https://docs.microsoft.com/en-us/sharepoint/administration/export-a-site-list-or-document-library
  2. http://blog.falchionconsulting.com/index.php/2007/09/importexportcopy-lists/

Prevent Special Characters in Single line text field

One of the data validation challenges is prevent the users from entering special characters in text field (Single line of text) . In this validation formula, nested if & isError will be used as in the following formula

=AND(IF(ISERROR(FIND(“,”,[ColumnName])),TRUE), IF(ISERROR(FIND(“&”,[ColumnName])),TRUE), IF(ISERROR(FIND(“;”,[ColumnName])),TRUE), IF(ISERROR(FIND(“[“,[ColumnName])),TRUE), IF(ISERROR(FIND(“+”,[ColumnName])),TRUE), IF(ISERROR(FIND(“:”,[ColumnName])),TRUE), IF(ISERROR(FIND(“)”,[ColumnName])),TRUE), IF(ISERROR(FIND(“-“,[ColumnName])),TRUE), IF(ISERROR(FIND(“*”,[ColumnName])),TRUE), IF(ISERROR(FIND(“(“,[ColumnName])),TRUE), IF(ISERROR(FIND(“$”,[ColumnName])),TRUE), IF(ISERROR(FIND(“%”,[ColumnName])),TRUE), IF(ISERROR(FIND(“~”,[ColumnName])),TRUE), IF(ISERROR(FIND(“#”,[ColumnName])),TRUE), IF(ISERROR(FIND(“]”,[ColumnName])),TRUE), IF(ISERROR(FIND(“.”,[ColumnName])),TRUE), IF(ISERROR(FIND(“!”,[ColumnName])),TRUE), IF(ISERROR(FIND(“@”,[ColumnName])),TRUE), IF(ISERROR(FIND(“/”,[ColumnName])),TRUE), IF(ISERROR(FIND(“\”,[ColumnName])),TRUE))

Also, the same formula can be used to prevent numeric values to be entered in the text field and replacing the characters with the numbers.

I hope this will be helpful for the SharePoint developers.

Enjoy single line of text without special characters

How to make SharePoint Attachment required

List item attachment in many cases needs to be required and at the same time the developer doesn’t want to edit the form using SharePoint designer. The trick is pretty easy, just edit the newForm.aspx to the list in browser and add content editor. Then, copy and paste the following script.

Note: you can change the message in the alert

<script type="text/javascript" language="javascript">
 
function PreSaveAction() {
if (document.getElementById('idAttachmentsRow').style.display=='none' )
  {
     alert('Attachment is Mandatory! Please attach Documents.');
     return false ;
  }
else {  return true;  }
}
</script>

Enjoy the required attachment with the alert.

E-mail Validation

SharePoint developers are facing many challenges in the data validations. One of these challenges is validating the email address. Emails are stored in SharePoint column with data type single line text and this will allow the users to enter any character series with maximum number 255 which will require data validation.

  1. Validate if the e-mail column includes . & @

=AND(IF(FIND(“@”,Email,2)>0,if(FIND(“.”,Email,2)>0,true,false),false)

2. Validate if the e-mail column includes . & @ and @ comes after . at least by 2 characters

=AND(IF(FIND(“@”,Email,2)>0,if(FIND(“.”,Email,FIND(“@”,Email,2)+2)>0,true,false),false)

3. Validate if the e-mail column includes . & @ and @ comes after . at least by 2 characters. Also, there is at least 2 characters after @

=AND(IF(FIND(“@”,Email,2)>0,if(FIND(“.”,Email,FIND(“@”,Email,2)+2)>0,if(FIND(“.”,Email,FIND(“@”,Email,2)+2)<len(Email),true,false),false),false)

Enjoy a valid email data entry.

LinQ to SharePoint

Introduction

LINQ is a feature of the programming languages C# and Microsoft Visual Basic .NET. Compilers are included with Visual Studio.

LINQ adds a SQL-like syntax and vocabulary to each of the languages, which can be used to query data sources. But unlike other languages and query syntaxes which vary from one type of data source to another, LINQ can be used to query, in principle, any data source whatsoever. For this reason, developers may find that it is the only query syntax that they ever need to know.

The LINQ to SharePoint Provider

The LINQ to SharePoint Provider is defined in the Microsoft.SharePoint.Linq namespace. It translates LINQ queries into Collaborative Application Markup Language (CAML) queries. It is no longer necessary for developers to know how to write CAML queries. LINQ queries can be used in server code.

The gateway class for the LINQ to SharePoint provider is Microsoft.SharePoint.Linq.DataContext which represents the data of a SharePoint Foundation Web site. It is parallel in use and function to the System.Data.Linq.DataContext class in the LINQ to SQL provider.

References:

https://docs.microsoft.com/en-us/previous-versions/office/developer/sharepoint-2010/ee535491(v=office.14)

Backup and Restore web applications

To backup SharePoint web applications :

  1. using PowerShell scripts, the following command shall be used

Backup-SPSite https://server_name/sites/site_name -Path C:\Backup\site_name.bak

2. using central administration:

To Restore SharePoint web applications:

  1. Central administration –> create new web application
  2. PowerShell script –> execute the following command

Restore-SPSite https://server_name/sites/site_name -Path C:\Backup\site_name.bak

Or the detailed command can be used in order to identify the database name and the server name

Restore-SPSite https://server_name/sites/site_name -Path C:\Backup\site_name.bak -Force -DatabaseServer SQLBE1 -DatabaseName SQLDB1

References:

https://docs.microsoft.com/en-us/powershell/module/sharepoint-server/backup-spsite?view=sharepoint-ps

https://docs.microsoft.com/en-us/powershell/module/sharepoint-server/restore-spsite?view=sharepoint-ps

Install and deploy custom solutions

Install SharePoint solution:

The cmdlet includes many parameters but the most commonly used is the following one:

Add-SPSolution -LiteralPath “physical path\solution name.wsp”

ex: Add-SPSolution -LiteralPath “c:\SolutionFolder\Solutionname.wsp”

Deploy SharePoint solution:

  1. deploy the solution globally into the farm:
    • Install-SPSolution -Identity solutionname.wsp -GACDeployment
  2. deploy the solution to specific web application:
    • Install-SPSolution -Identity solutionname.wsp -WebApplication “http://webappURL” -GACDeployment
  3. deploy the solution and identify the compatibility level (this is useful while upgrading the solution):
    • Install-SPSolution -Identity solutionname.wsp -GACDeployment -CompatibilityLevel {14,15}
  4. Deploy the solution using the central administration:
    • Login into central administration as the farm admin
    • Navigate to system settings and choose farm solutions
    • Check the solution list, the recently added one will have status “not deployed” choose and select the web application.
    • Press deploy

That was a summary of the needed cmdlet to install and deploy SharePoint custom solutions