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();
                    }
                }
            }
        }

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)

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)