gogi100 avatar image
0 Votes"
gogi100 asked RaytheonXie-MSFT commented

event receiver in combination with powershell

i have powershell script that upload mulitple pdf documents in the sharepoint 2010 document library in folders. this code is

 [System.Reflection.Assembly]::LoadFrom("D:\readpdf\itextsharp.dll") | Out-Null
 Add-Type -Path "D:\readpdf\itextsharp.dll" 
 $destinationFolders = Get-ChildItem Z:\
 $files = Get-ChildItem D:\readpdf\listici -Filter *.pdf -Recurse 
 foreach ($file in $files) 
         #Open the file
     $reader = New-Object iTextSharp.text.pdf.pdfreader -ArgumentList $file.FullName
     #Formatiranje PDF-a da bi se dobio izlaz koji omogucava da se uporedi ID zaposlenog sa imenom foldera
     $page = 1;
       $text = [iTextSharp.text.pdf.parser.PdfTextExtractor]::GetTextFromPage($reader,$page).Split([char]0x000A)
      $kb = $text[4] -split{$_ -eq ":"}
      $kb[1] = $kb[1] -replace '\s',''
  # Write-Host "Page $($page) contains $($text.Length) lines. This is line 5:"
  #Write-Host $kb[1]
              #petlja za listanje foldera na sharepoint-u   
               foreach ($folder in $destinationFolders) 
                     $destPath = $folder.FullName
                     $kbfoldername = $folder.Name -split{$_ -eq "_"}
              #provera da li se ime foldera poklapa sa ID zaposlenog u dokumentu                 
             if($kb[1]  -eq $kbfoldername[1] ) 
        #    $lines[$i]
         #   $folder.Name
              #kopiranje fajla u folder cije se ime poklapa sa ID zaposleno
                      Copy-Item $file.FullName $destPath -force
                  }# for folder
   }#for file

Also, i created event receiver on this document library that updating field of file base on field values of parent folder. this code is

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.Administration;
using System.Collections.Specialized;

namespace licna_akta.licna_akta
/// <summary>
/// List Item Events
/// </summary>
public class licna_akta : SPItemEventReceiver
/// <summary>
/// An item was added.
/// </summary>
public override void ItemAdded(SPItemEventProperties properties)
this.EventFiringEnabled = false;
using (SPWeb web = properties.OpenWeb())
SPFile file = properties.ListItem.File;
SPFolder parentFolder = file.ParentFolder;
string poljekorisnickoime = parentFolder.Item["Корисничко име"] == null ? "" : parentFolder.Item["Корисничко име"].ToString();
string poljeimeiprezime = parentFolder.Item["Име и презиме"] == null ? "" : parentFolder.Item["Име и презиме"].ToString();
string poljemail = parentFolder.Item["Е-пошта"] == null ? "" : parentFolder.Item["Е-пошта"].ToString();
properties.ListItem["Корисничко име"] = poljekorisnickoime;
properties.ListItem["Име и презиме"] = poljeimeiprezime;
properties.ListItem["Е-пошта"] = poljemail;
if (!(poljemail == ""))
SendMail(web, "", poljemail, "Лична акта - обавештење о пребаченом документу", "Ваш документ " + properties.ListItem["Име"] + " је пребачен. Локација документа је " + web.Url + '/' + properties.ListItem.Url, "", "");

        this.EventFiringEnabled = true;

    public void SendMail(SPWeb web, string from, string to, string subject, string body, string cc, string bcc)
            StringDictionary headers = new StringDictionary();
                string mail = to;

                headers.Add("from", from);
                headers.Add("to", mail);
                headers.Add("subject", subject);
                if (!String.IsNullOrEmpty(cc)) headers.Add("cc", cc);
                if (!String.IsNullOrEmpty(bcc)) headers.Add("bcc", bcc);
                headers.Add("content-type", "text/html");
                SPUtility.SendEmail(web, headers, body);
        catch (Exception exc)
            // a custom method to write error messages to ULS
            //  SPULSTraceListener traceULS = new SPULSTraceListener();
            SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("Failed - sending mail", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, exc.Message, exc.StackTrace);





Element.xml is

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="">;
<Receivers ListUrl="DocLib12">


the powershell script is working ok, but the event receiver does not update field value of uploded document. how i resolve this problem

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

RaytheonXie-MSFT avatar image
0 Votes"
RaytheonXie-MSFT answered RaytheonXie-MSFT commented

Hi @gogi100 ,
In sharepoint 2010, you need to enable CustomEvent Feature in site settings.

After develop your Event Receiver by PowerShell Script

 Add-SPSolution full path of the solution file
 Activate the solution: Install-SPSolution solution-name.wsp –GACDeployment –AllWebApplications
 Active the feature: Enable-SPFeature FeatureFolderName -Url http://server/site/subsite

If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

[2]: /answers/storage/attachments/189674-image.png

image.png (14.9 KiB)
image.png (14.9 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @gogi100 ,
Have you tried the solution I proposed?

If you have any questions or progress, you can contact me in time.

Looking forward to your reply

Have a lucky day!

Raytheon Xie

0 Votes 0 ·

Hi @gogi100 ,
Did the answer help you? If you have any question about the issue, please feel free to reply.

0 Votes 0 ·