Friday, 20 May 2016

PnP Core Component - Check if field exists in site by name in SharePoint 2016

Please refer Introduction to PnP Core Component for more details. I have created a console application and added SharePointPnPCoreOnline NuGet package for SharePoint 2016 version.

FieldExistsByName(Microsoft.SharePoint.Client.Web,System.String)
Returns if the field is found

Parameters

web: Site to be processed - can be root web or sub site. Site columns should be created to root site.

fieldName: String for the field internal name to be used as query criteria

Code Snippet: 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using OfficeDevPnP.Core;

namespace SP2016PnPCoreComponentDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            // Input Parameters
            string siteUrl = "http://c7395723754:35298/sites/VijaiDemo";
            string userName = "administrator";
            string password = "xxxxxxxx";
            string domain = "AD2012";
            string fieldName = "PnP Text";

            OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();

            try
            {
                // Get the client context
                using (var ctx = authMgr.GetNetworkCredentialAuthenticatedContext(siteUrl, userName, password, domain))
                {
                    // Check if field exists by Name using CSOM Extension Method  
                    if (ctx.Web.FieldExistsByName(fieldName))
                    {
                        Console.WriteLine(fieldName + " exists in the site");
                        Console.ReadLine();
                    }
                }
            }

            catch (Exception ex)
            {
                Console.WriteLine("Error Message: " + ex.Message);
            }
        }
    }
}

Saturday, 24 September 2011

Programmatically get all the site collections for a particular managed path in SharePoint

In this blog we will be seeing how to get all the site collections for a particular managed path in Sharepoint

using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
Microsoft.SharePoint;
using
Microsoft.SharePoint.Administration;
namespace
GetAllSCforManagedPath
{
class Program
{
static void Main(string[] args)
{
using (SPSite currentSite = new SPSite("http://serverName:1111/hr/MP3/"))
{
string relativerURL = currentSite.ServerRelativeUrl.ToString(); string[] split =relativerURL.Split('/'); string managedPath = split[1].ToString(); SPSiteCollection siteColl = currentSite.WebApplication.Sites; foreach (SPSite site in siteColl)
{
if (site.ServerRelativeUrl.Contains(managedPath) == true)
{
Console.WriteLine(site.Url.ToString());
}
}
Console.ReadLine();
}
}
}
}