Sunday 22 November 2015

How to Covert invalid file name to valid file name format, with unique file name, get valid file name, file name validation


The most common piece of code we used to put for file name validation before uploading, we need to remove invalid characters as well made the same file as unique file name by appending Current DateTime string.  

Now lets's see how to get desired file name to save on drive or some location.


Problem The Keyboard have hell lot of special characters and few of them are invalid. 
That makes issue sometime for application developer while writing or creating file.



Solution User entered some incorrect word so instead of giving error to end user we can replace all those special characters by any specific character like I have done with char '_' underscore,
The file name is very important in terms of search or crawl files/file name. 


private string GetValideUniqueFileName(string fileName)
        {
            char[] additionalInvalidSharePointSpecialCharacters = new char[8] { '~', '#', '%', '&', '{', '}', '+', ' ' };
            char[] defaulChars = System.IO.Path.GetInvalidFileNameChars();

            defaulChars = defaulChars.Concat(additionalInvalidSharePointSpecialCharacters).ToArray();

            foreach (char item in defaulChars)
            {
                fileName = fileName.Replace(item, '_');
            }
            
 string name = string.Format ("{0}_{1}{2}",System.IO.Path.GetFileNameWithoutExtension(fileName),
     DateTime.Now.ToString("yyyy-MM-dd-hh_mm_ss"),
     System.IO.Path.GetExtension(fileName))

            return name;
        }


Any better solution or enhancement needed here, so put your thoughts in comment box

Thursday 30 July 2015

Update Modified by and Created by in SharePoint list

Some of you have face the situation to update list with large number of items or some time we have to change, so that lead to change Modified By to your name; Modified or created by names of all items, yeah we are aware about System.Update() method but if data has populated and no code action will take place and requirement says to update because we don't wants to confuse client/Business users and his customers so in very rare and after approval we execute it to update modified and created by names.

We can use below script to update thousands of list items in single shot. but take care once changes are done by PowerShell can not be roll back. 

so to update SharePoint list items simply can save below PowerShell script in a .PS1 file and run it with Elevated privileges in SharePoint server where you has permission to modify items. 

PowerShell:


<# Created by: MakrShortWork
   Purpose:   To update Modified and created by Names.
   Created Date: June, 2011
#>

  $web = Get-SPWeb "http://YourWebUrl"
  $spList =  $web.Lists["ListName"]  
  $ListItemID = 23;
  $UserName =$ web.EnsureUser("domain\useridLoginId");  
     
  $caml = '<Where><Eq><FieldRef Name="ID" /><Value Type="Text">{0}</Value></Eq> </Where>' -f $ListItemID
  $query = new-object Microsoft.SharePoint.SPQuery
  $query.Query=$caml
  $oItem = $spList.GetItems($query)[0]

# in List Created by -Author and Modifed by -Editor
     $oItem["Author"] = $UserName           
     $oItem["Editor"] = $UserName  

     # You can add other fields here any field value data time etc....
     $oItem.Update()
    # You can use SystemUpdate(true/false);

Please run script on SharePoint server with Admin privileges,
Share or refer the link, that may help some one

-