Wednesday, June 3, 2020

PowerShell: Migrate SharePoint list content from one farm to other farm using CSOM

Leave a Comment
Scenario:
I met a scenario, where I need to migrate some list items (around 500+) from one farm (development) to another farm (Staging) and then to Production farm.

Ideal scenario probably would be to create list schema with content. However, in my current scenario I used CSOM for copying list items. Also in my scenario I just can't replace current content of destinate list item. I must have to just copy/move list items which are required for newly developed components.

Solution:

First I save list as list template along with content from source farm and create new list in destination farm using saved & imported list template definition. Then I delete list items which is not supposed to be copied. Although this can be skipped or cater through power shell script. However, due to quick delivery I wrote following power shell script which serve my purpose.

#Since I am doing for On-Premises, so I must have to import Client runtime for On-Premises
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Here I am getting user credentials
$siteURL="http://SomeSiteUrl/"
$ctx=New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
$pwd=Read-Host -Prompt "Enter password" -AsSecureString
$creds=New-Object System.Net.NetworkCredential("domain\UserName",$pwd)
$ctx.Credentials=$creds

$sourceList=$ctx.Web.Lists.GetByTitle("[{ImportedListFromDestinationFarm}]")
$destList=$ctx.Web.Lists.GetByTitle("[{SourceListWhereDataNeedToCopied}]")
$sourceListItems=$sourceList.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
$fields=$sourceList.Fields
$ctx.Load($sourceListItems)
$ctx.Load($sourceList)
$ctx.Load($destList)
$ctx.Load($fields)
$ctx.ExecuteQuery() 

#Here I am making sure, fields of list item isn't hiddent, or not readonly & don't have attachment
#and then updating in destination list
foreach($item in $sourceListItems)
 {
 #Write-Host $item.ID
$listItemInfo=New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
$destItem=$destList.AddItem($listItemInfo)
foreach($field in $fields)
{
#Write-Host $field.InternalName "-" $field.ReadOnlyField
if((-Not($field.ReadOnlyField)) -and (-Not($field.Hidden)) -and ($field.InternalName -ne "Attachments") -and ($field.InternalName -ne "ContentType"))
{
#Write-Host $field.InternalName "-" $item[$field.InternalName]
$destItem[$field.InternalName]=$item[$field.InternalName]
$destItem.update()
} }}
$ctx.ExecuteQuery()
Read More

Sunday, January 12, 2020

Export AD user to csv file

Leave a Comment
This blog post is kind of immediate requirement to import users from one dev AD to another testing AD. So it include of two parts.

Part 1: To export users from Dev AD to csv file
Part 2: To import users to testing AD from csv file

In this post we will see how to export users from particular OU to a CSV file.

Following is PowerShell script which I used:

#OU Name/path whom users need to be exported
$OUName = 'ou=OUName,dc=domain,dc=com'

#Path where csv file will be created with OU users
$ExportPath = 'c:\tmp\myOU_Users.csv'

#command to export users with selected properties
Get-ADUser -Filter * -SearchBase $OUName -Properties GivenName, EmailAddress,di
splayname,Name,CN,OU,Surname,Title,sAMAccountType,samaccountname | Export-Csv $ExportPath

This will export uses from required OU to csv files
Read More

Sunday, December 22, 2019

Download deployed WSP Solutions from SharePoint farm

Leave a Comment
While working with for a client, whom project previous vendor left in middle without proper documentation, project plan & updated source code. Only option left for me is to download deployed WSP Solutions from Production farm and go through deployed Dll through tools like ILSpy etc.

To download we used following powershell commands:

$farm=Get-SPFarm
$file=$farm.Solutions.Item("DEPLOYED_WSP_NAME.wsp").SolutionFile
$file.SaveAs("C:\SomeDownloadLocation\DEPLOYED_WSP_NAME.wsp")
Read More

The local farm is not accessible. Cmdlets with FeatureDependencyId are not registered

Leave a Comment
While trying to run SharePoint powershell on vendor VM, found this issue: "

The local farm is not accessible. Cmdlets with FeatureDependencyId are not registered"


Solution:

We need to run with domain account by which we setup SharePoint farm and run following command:

Add-SpShellAdmin -username Domain\username.

Above command set appropriate permission automatically and this can be verified by Get-SPShellAdmin
Read More

Sunday, August 25, 2019

Configure Gmail as SMTP for SharePoint 2016 server to relay messages

Leave a Comment
This blog post is about configuring gmail as smtp for SharePoint 2013/2016/2019 to relay email messages on behalf of SharePoint.

First of all we need to configure SMTP server on Windows server (Currently I am using Windows Server 2016 Standard)

If IIS Manager 6 feature/role isn't setup first we need to set it up from Add server roles & features.

Once configured, open IIS 6 Manager and right click on SMTP Virtual server and click properties:

Until we want SMTP server to use specific IP address, leave the setting as it is in General tab
Then click Access tab
Make sure by clicking Authentication button that anonymous access is selected
On Access tab click on connection button and select second option or choose first option with your server IP address added, then click OK.
On Access tab click on Relay and choose All except list below option as shown below
Then click on delivery tab, and choose Outbound security button and select Basic authentication and enter gmail credentials and check TLS encryption and click OK.
In the Outbound connection, enter 587 as TCP Port and click OK.
In the delivery tab click Advanced button and fully qualified domain name FQDN and smart host as smtp.gmail.com
Then login to gmail account which we are using as SMTP relay, click on My Account and then Sign in security  and select Allow less secure apps to ON position

Don't forget to update POP UP setting of gmail account

Although gmail SMTP as relay has been completed and can be verified by using a simple Powershell command/script as below:

Just to explain, here from address can be used as any address but email will always be come from your gmail smtp relay email address we used above.

In order to complete SharePoint outgoing email, in Centeral Admin of SharePoint, click System Settings and Outgoing email settings



Read More

Wednesday, August 7, 2019

SharePoint: How to verify current logged in user belong to which SharePoint Group

Leave a Comment
There was a scenario, we during our development cycle faced multiple time that we need to verify currently logged in user in SharePoint for its role or SharePoint Group. As based on role we have different business rules & business process automation. So following is server side code for verifying currently logged in user (either Active directory based or form based authtentication) belong to which SharePoint Group:

//Let user be authenticated first into SharePoint
SPClaimsUtility.AuthenticateFormsUser(
                       new Uri(SPContext.Current.Web.Url), username, password);                   
                       
                        SPUser spUser = SPContext.Current.Site.RootWeb.EnsureUser(SPContext.Current.Web.CurrentUser.LoginName);

                        //check logged in user belong to which SharePoint group/role
                        SPSecurity.RunWithElevatedPrivileges(delegate ()
                        {

                            //SPSite site = SPContext.Current.Site;
                            using (SPSite site = new SPSite(SPContext.Current.Web.Url))
                            {
                                using (SPWeb web = site.OpenWeb())
                                {                                   
                                    if(spUser!=null)
                                    {
                                        var userGroups = spUser.Groups;
                                        foreach (SPGroup group in userGroups)
                                        {
                                            if (group.Name.ToLower().Equals("role1"))
                                            {
                                                userGroup = "role1";
                                            }
                                            else if (group.Name.ToLower().Equals("role2"))
                                            {
                                                userGroup = "role2";
                                            }
                                            else if (group.Name.ToLower().Equals("role3"))
                                            {
                                                userGroup = "role3";
                                            }
                                        }
                                    }                                 
                                }
                            }
                        });
Read More

Monday, July 15, 2019

Which application pool id link with which worker process (w3wp.exe)

Leave a Comment
Often there are times when we need to debug our application by Debug-> attach to process option in visual studio. Here we need to figure out which application pool is currently linked with our application. So to figure out run following command:

C:\Windows\System32\inetsrv>appcmd list wp


Read More