Search This Blog

Sunday, June 18, 2017

How to change list and document library web URL In sharepoint using powershell

I this post we will see how to change the list and document library url using powershell.

            We have some scenarios like have to change the list name for an existing list, we can change the name of the list on the list settings page, Select List name, description, and navigation option under the General settings.but we couldn't able to change the web url using User Interface,but we can achieve this using below powershell script.

For this we need Microsoft Powershell Management shell, open the PMS using run as administrator privileges and use the below powershell script to change the web URL of the list/document library.

Add-PSSnapin Microsoft.SharePoint.Powershell
#Paste the site or subsite url
#If you are using site collection url using Get-Spsite instead of Get-Spweb
$Web = Get-SPWeb "http://Test:8080/sites/TestSite/SubSite/"
#Mention the name of the to which the web url should change
$List = $Web.Lists["TestList"]
#Paste the new url which we prefer to change 
$List.RootFolder.MoveTo("/sites/TestSite/SubSite/Lists/test") 

Saturday, June 17, 2017

How to backup the deployed wsp using powershell

The below powershell code is used to get the backup of deployed wsp solution

$farm = Get-SPFarm
$file = $farm.Solutions.Item("MySolution.wsp").SolutionFile
$file.SaveAs("c:\MySolution.wsp")  

Export webpart from sharepoint page

In this post we will see how to export webpart from the sharepoint page.

First we have to find the webpart id of the webpart which we are going to export,For that  press f12 in you webbrowser and inspect the webpart and find the webpart Id.

In SharePoint there is a hidden application page that exports web parts: /_vti_bin/exportwp.aspx. This page takes two query parameters:

pageurl. The absolute url of the page where the web part resides that you want to export
guidstring. The guid that is called webpartid in the markup on the page

So, suppose, you have this site: https://parthasarathybalan.sharepoint.com and a web part (id: 66509661-e83d-4e18-9f74-07c04c8e5a79') on a page https://intranet.contoso.com/Pages/Home.aspx

This will be the resulting URL to export your webpart:

https://parthasarathybalan.sharepoint.com/sites/Testsite/_vti_bin/exportwp.aspx?pageurl=https://parthasarathybalan.sharepoint.com/sites/Testsite/SitePages/Home.aspx&guidstring='66509661-e83d-4e18-9f74-07c04c8e5a79'

How to get value from multilinetext field using csom

//The below code is used to retrieve value from multiline text field using csom

static void Main(string[] args)
{
 string siteUrl = "http://domainname/sites/TestSite";
 using(ClientContext clientContext = new ClientContext(siteUrl))
 {
    var web = clientContext.Web;
    var list = web.Lists.GetByTitle("Test List");
    var listFields = list.Fields;
    clientContext.Load(listFields);
    var listItem = list.GetItemById("1");
    var itemFieldValues = listItem.FieldValuesAsText;
    clientContext.Load(itemFieldValues);
    clientContext.ExecuteQuery();
    string multitextValue = Convert.ToString(itemFieldValues["MultiLineTextField"]);
 }
}

Saturday, June 10, 2017

How to resolve time zone in sharepoint 2013

The below method is used to resolve time zone in sharepoint 2013

public static DateTime ResolveTimeZone(ClientContext oParentContext, DateTime dtDate)
{
   //DateTime dtStartDate = DateTime.UtcNow;
  try
  {
    var spTimeZone = oParentContext.Web.RegionalSettings.TimeZone;
    oParentContext.Load(spTimeZone);
    oParentContext.ExecuteQuery();
    var fixedTimeZoneName = spTimeZone.Description.Replace("and", "&");
    var timeZoneInfo = TimeZoneInfo.GetSystemTimeZones().FirstOrDefault(tz => tz.DisplayName == fixedTimeZoneName); //"(UTC-08:00) Pacific Time (US and Canada)"
    TraceUtil.TraceMessage("UTC= " + dtDate);
    TraceUtil.TraceMessage("TZ= " + timeZoneInfo);
    dtDate = TimeZoneInfo.ConvertTimeFromUtc(dtDate.Date.ToUniversalTime(), timeZoneInfo);
    dtDate = dtDate.Date + ts;
  }
  catch (Exception ex)
  {
     throw ex;
  }

  return dtDate;
}

How to Disable Event Firing on List Item Update in SharePoint 2013 Programmatically

public classEventFiring : SPItemEventReceiver
{
  public void DisableHandleEventFiring()
  {
     this.EventFiringEnabled =false;
  }

  public void EnableHandleEventFiring()
  {
     this.EventFiringEnabled =true;
  }
}


class Program
{
  static void Main(string[] args)
  {
    using (SPSite site = new SPSite("https://serverName/sites/testsite/"))
    {
      using (SPWeb web = site.OpenWeb())
      {
        SPList list = web.Lists.TryGetList("Test List");
        SPListItem item = list.GetItemById(1);
        item["Title"] ="Updated Successfully";
        EventFiring eventFiring = newEventFiring();
        eventFiring.DisableHandleEventFiring();
        item.Update();
        eventFiring.EnableHandleEventFiring();
        Console.WriteLine("Updated Successfully");
        Console.ReadLine();
     }
   }
  }
}

Authenticating .NET Client Object Model in Office 365 using shareepoint online

The below code is used to authenticating sharepoint online site

using (ClientContext clientContext = new ClientContext("https://yoursite.sharepoint.com/"))
{
    SecureString passWord = new SecureString();

    foreach (char c in "yourpassword".ToCharArray()) passWord.AppendChar(c);

    clientContext.Credentials = new SharePointOnlineCredentials("admin@yoursite.onmicrosoft.com", passWord);

    Web web = clientContext.Web;

    clientContext.Load(web);

    clientContext.ExecuteQuery();

    Console.WriteLine(web.Title);

    Console.ReadLine();
}

Method to get timestamp in c#

 public static string GetTimeStamp()
 {
     return DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-fff");
 }

How to read listitem attachments using sharepoint client object model

The below method is used to get the files which is attached in the listitem in the custom list

public static FileCollection GetListItemAttachments( string sitePath, string listName, string itemID)
{
   try
   {
      FileCollection oFileCollection=null;
      Folder attachmentsFolder = null;
      ClientContext clientContext = new ClientContext(sitePath);
      Web web = clientContext.Web;
      clientContext.Load(web);
      clientContext.ExecuteQuery();

      attachmentsFolder = web.GetFolderByServerRelativeUrl(sitePath + "/Lists/" + listName + "/attachments/" + itemID);
      clientContext.Load(attachmentsFolder);
      oFileCollection = attachmentsFolder.Files;
      clientContext.Load(oFileCollection);
      clientContext.ExecuteQuery();
   
      return oFileCollection;
   }
   catch (Exception ex)
   {
     throw ex;
   }
 }

How to remove registered app from sharepoint online

In this post i will explain how to remove registered app from sharepoint online.

For this please install Azure PD Powershell before going through this post.

Open the  Installed  Azure PD Powershell with run as Administrator permission.and run the following sript to remove the app from sharepoint online.

Connect-MsolService
$appPrincipal = Get-MsolServicePrincipal -ServicePrincipalName 'Paste your app client id here'
Remove-MsolServicePrincipal -ObjectId $appPrincipal.ObjectId

After executing the above script please register your app again in 
"SiteUrl/_layouts/15/appregnew.aspx"