Monday, March 18, 2013

Get-SPWeb object dispose Powershell

here is the good post

http://www.ilovesharepoint.com/2009/11/sharepoint-2010-powershell-and.html

Enable Versioning SharePoint 2010 Document Library reading CSV file using Powershell


My requirement is update all document libraries version history “No Version” to “Major version”. when i loop through all document libraries from all sites it is updating so many out of box document libraries. I dont want to touch any out of box or other libraries.  so i come up with one csv file which is having my document library names and site relative url as shown below.


here DocLibName, URL are headings. for home site you dont need to specify any value for URl like below.



copy to notepad file and Save as DocLibraries.csv file. dont change below heading names.



DocLibName
 URL
Shared Documents

Resource Library
subsite1
Meeting Minutes
sbusite2
Archives
subsite3
Final Documents
susbsite3


$URL = "http://spsev5:8003/"
#add-pssnapin Microsoft.SharePoint.Powershell

$site = New-Object Microsoft.SharePoint.SPSite($URL)
#$web = $site.openweb("")
#.csv file path
$InputFile = "D:\Development\Surya\DocLibraries.csv"
$file = ipcsv $InputFile

#logging
$timestamp = get-date -format "yyyyMMdd_hhmmtt"
$filenameStart = "DocLibraryVersioningLog"
$logfile = ("{0}{1}.csv" -f $filenamestart, $timestamp)
$header = "Head1,Head2"
$header | out-file -FilePath $logfile

#variables
      $csvDocLibName= $null
      $csvURL = $null
      $webUrl = $null
      $i=1
     
Write-Host "Started"
foreach ($line in $file)
{
#get excel sheet cell values 
   $csvDocLibName = $line.DocLibName
   $csvURL=   $line.URL
   $webUrl=  "$URL$csvURL" #concatenating site url + subsite url from csv file
     
      #Write-Host $webUrl
      #Write-Host "DocLibName:"  $DocLibName "URL:" $webURL
     
try  {

            Start-SPAssignment -Global
         $web =Get-SPWeb  $webUrl
            $msg = ("{0},{1}" -f "$i", "$webUrl")
            $msg | out-file -FilePath $logfile  -append
           
            $library = $web.Lists[$csvDocLibName]
           
            if($library  -ne $null)
            {
                  Write-Host "library name : " $library.Title
               $library.EnableVersioning = $true
            # $library.EnableMinorVersions = $true
             #$library.MajorVersionLimit = $majorVersionLimit
             #$library.MajorWithMinorVersionsLimit = $minorVersionLimit
             #$library.ForceCheckOut = $requireCheckOut
             $library.Update()
            }
           
            Write-Host "....library version updated : " $web.URL  $library.Title
            $msg = ("{0},{1}" -f "......library version updated:", $library.Title)
            $msg | out-file -FilePath $logfile  -append
           
            Stop-SPAssignment -Global
            #$web.Dispose()
           
      }
Catch
      {
            # $_ | Out-File d:\surya\errors.txt -Append
          $msg = ( "{0},{1}" -f "Error occurred while updating version:", $_)
            $msg | out-file -FilePath $logfile  -append
      }
     
      $i++
           
}
$web.Dispose()
$site.Dispose()






Friday, March 15, 2013

CAML Query Issues SharePoint


CAML Builder code
<Query><Where><And><Eq><FieldRef Name="Created" /><Value Type="DateTime">[Today+1Day(s)]</Value></Eq></And></Where></Query>

Remove <Query> node as shown below in the coding

<Where><And><Eq><FieldRef Name="Created" /><Value Type="DateTime">[Today+1Day(s)]</Value></Eq></And></Where>


CAML Builder code
<Value Type="DateTime">[Today+1Day(s)]</Value>

Update as shown below in the coding

Get offsetdays value at runtime
<Value Type='DateTime'><Today OffsetDays='" + offsetdays + "'/></Value>

You can hard code like this

<Today OffsetDays='5'/></Value>


Below format is not supporting some times in the coding

   <Where>
      <And>
         <Eq>
            <FieldRef Name='Created' />
            <Value Type='DateTime'><Today OffsetDays='5'/></Value>
         </Eq>
      </And>
   </Where>

Always use below format in the coding

<Where><And><Eq><FieldRef Name="Created" /><Value Type="DateTime><Today OffsetDays='5'/></Value></Eq></And></Where>


   
Sample caml query function

        private static string CreateCAMLQuery(string symbol,string offsetdays,string redcnt)
        {
            string strQuery = string.Empty;

            switch (symbol.ToLower())
            {
                case "green":
                    strQuery = "<Where><And><IsNull><FieldRef Name='Actual_Date' /></IsNull><Geq><FieldRef Name='Target_Date' /><Value Type='DateTime'><Today OffsetDays='" + offsetdays + "'/></Value></Geq></And></Where>";
                    return strQuery;
                case "yellow":
                    strQuery = "<Where><And><IsNull><FieldRef Name='Actual_Date' /></IsNull><And><Gt><FieldRef Name='Target_Date' /><Value Type='DateTime'><Today OffsetDays='" + redcnt + "'/></Value></Gt><Leq><FieldRef Name='Target_Date' /><Value Type='DateTime'><Today OffsetDays='" + offsetdays + "'/></Value></Leq></And></And></Where>";
                    return strQuery;
                case "red":
                    strQuery = "<Where><And><IsNull><FieldRef Name='Actual_Date' /></IsNull><Leq><FieldRef Name='Target_Date' /><Value Type='DateTime'><Today OffsetDays='" + offsetdays + "'/></Value></Leq></And></Where>";
                    return strQuery;
                default:
                    return null;
            }
        }


Call caml query function
    if (item["Days_x0020_for_x0020_Green"] != null)
                                           {
                                               queryGreen.Query = CreateCAMLQuery("green", item["Days_x0020_for_x0020_Green"].ToString(), "0");
                                               SPListItemCollection itemsGreen = list.GetItems(queryGreen);
                                               drow1[1] = "/_layouts/images/KPIDefault-0.GIF";
                                               drow1[2] = itemsGreen.Count.ToString();
                                           }


Wednesday, March 13, 2013

Copy files from local folder to SharePoint Document Library using PowerShell

here is the post

http://suryapulipati.blogspot.com/2013/03/move-files-from-network-drive-to.html

Move files from Network Drive to SharePoint using PowerShell


By using below script you can move your network drive or local folder files to SharePoint.
for more information on this you can visit this site

it will copy files sharepoint without metadata.


you can copy all files to sharepoint with metadata






Friday, March 1, 2013

Bulk Delete Asp.Net Membership Users


You can use below select statement according to your requirement. Please use one of the select statement. Be care full before running the script as it deletes all the users based on where condition.



SELECT UserID, Email
INTO #temp
FROM aspnet_Membership
WHERE Email like '%tech%'

or

SELECT UserID, UserName
into #temp
FROM aspnet_Users
WHERE UserName in ('test')


DELETE FROM dbo.aspnet_Membership WHERE UserId IN (Select UserId from #temp)
DELETE FROM dbo.aspnet_UsersInRoles WHERE UserId IN (Select UserId from #temp)
DELETE FROM dbo.aspnet_Profile WHERE UserId IN (Select UserId from #temp)
DELETE FROM dbo.aspnet_PersonalizationPerUser WHERE UserId IN (Select UserId from #temp)
DELETE FROM dbo.aspnet_Users WHERE UserId IN (Select UserId from #temp)

DROP TABLE #temp





Followers