Configuring SPO Tenant

In this post you are going to create a new tenant for use throughout all the examples that are mentioned in SPO-Admin – development categories. We are going to install the
necessary PowerShell tools for connecting to SharePoint Online and Azure Active Directory. Finally, you will create some users that will be used for testing during the course and install the Office client.

Prepare SharePoint management PowerShell:

  • Run Windows management Shell as admin
  • Type install-module MSOnline and press enter
  • Run the following commands in order:
          a. $Credential = get-credential – Enter your. steve@CkXXXX.onmicrosoft.com user name and password,
    click OK
         b. Import-Module MSOnline
          c. Connect-MsolService –Credential $Credentia
  • Get-MsolUse –> you should receive the user data such as principal name, display name & if the user is licensed or no

Connect to your tenant

Open notepad or any text editor and add the following commands:

  • Import-Module Microsoft.Online.SharePoint.PowerShell
  • Connect-SPOService -Url https://tenant-admin.domain/ -credential UserName
  • save the file with extension ps1
  • run the file from SharePoint online management shell.

Then you will receive a popup to enter the password

You can open SharePoint online Management shell and execute directly the connect command

Thousand separator in Numeric / Calculated field

SharePoint Online – By Heba Hegab

SharePoint list is the most widely used to store data. They have different types of data in order to help storing the data in correct format. Numeric data types has special format to display big numbers by using thousand separator, So, if you have a numeric column or calculated field with output numeric will suffer from this issue.

comma separator in calculated field

If you create column from list as numeric data type, you will have the option to remove the separator. But if you are adding from the site settings you won’t have this option.

Also, calculated field doesn’t have this option. So, you will need another way to solve this issue.

Now to create calculated field and remove the comma separator, we are going to use JSON to remove it. In the column settings, go to column validation and paste the following code

{“$schema”:”https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json”,”elmType”:”div”,”attributes”:{“class”:”=if(@currentField > 0,”, ”)”},
“children”:[{
“elmType”:”span”,”style”:{“display”:”inline-block”}},{“elmType”:”span”,”txtContent”:”@currentField”}
]}

Create Task in Task list – C#

SharePoint onPrem 2013, 2016, 2019

Create SharePoint list item has the same logic but for task list you will need assigned to.

Assigned to is site column if type people or hyperlink. So, it’s expecting to send by code a valid SharePoint user and this can be done by using EnsureUser and SPFieldUserValue .

EnsureUser –> will retrieve a valid SPUser

SPFieldUserValue –> will take the SPUser and retrieve and save the data in correct format.

using (SPSite site = new SPSite("https://serverName/sites/TEST"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists.TryGetList("Tasks");
                    if (list != null)
                    {
                        // get the SharePoint group
                        SPUser user = web.EnsureUser(@"domainName\userName");
 
                        // Create a new task item
                        SPListItem task = list.Items.Add();
                        SPFieldUserValue assignTo = new SPFieldUserValue(web, user.ID, user.Name);
                        task["AssignedTo"] = assignTo; 
                        task["Title"] = "Testing";
                        task["Description"] = "Testing";                       
                        task.Update();
                    }
                }
            }
        }

SharePoint feature

SharePoint onPrem 2013, 2016, 2019

Uninstall feature from SharePoint is one of the most important issue especially if you don’t know GUID for it. To uninstall the feature, you will need to get the feature first. Open SharePoint CMD as admin and use the following command

Get-SPFeature | where { $_.DisplayName.StartsWith("the feature name to be removed") }

Also, the feature can be found by GUID as in the following command

Get-SPFeature | where { $_.Id.ToString().StartsWith("Feature GUID") }

The complete command to uninstall the feature

Get-SPFeature | where { $_.DisplayName.StartsWith("feature name") } |
 foreach { Uninstall-SPFeature $_.Id -confirm:0 -force }

This command is very helpful especially when the retract didn’t go well and the feature became orphaned feature .

SPS outgoing e-mail

SharePoint 2013, 2016, 2019 OnPrem

In some scenarios, developers will need to test the SharePoint configuration for outgoing emails. Usually, this check will take place before implementing C# code to send emails from SharePoint web parts. The following script can be written inside ps file and run from powershell cmd.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Configuration Parameters
$SiteURL="http://sps:portNo/"
$Email = "receiver email"
$Subject = "Test Email from SharePoint"
$Body = "Test Email Body"
 
#Get the Web
$Web = Get-SPWeb $SiteURL
 
#Send Email using SPUtility SendEmail method
[Microsoft.SharePoint.Utilities.SPUtility]::SendEmail($Web ,0,0,$Email,$Subject,$Body)

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.