სწრაფი & მარტივი: სახელის დამატებულია ფაილი გამოყენებით SharePoint ობიექტური მოდელის ვია თარიღის მიმღები

განახლების: This works but there are significant limitations which are described in the comments. This may still be useful in some cirumstances.

განახლების 2: ჩემი მიმდინარე პროექტის, users always upload documents. შედეგად, I don’t run into a problem where MS Word is running and thinks that the file was renamed on it. I did run into a problem, "the file was modified by someone else" and solved this via a simple semaphore type flag. Users need to change a meta data field from its default value to something else. The itemupdated() მიმღები ეძებს მოქმედი ღირებულება არსებობს, სანამ რეალურად ასრულებენ გადარქმევა და მას შემდეგ, I have not had any problems. Your mileage may vary.

I have a client requirement to change the name of files uploaded to a specific document library to conform with a particular naming convention. The API does not provide a "rename()" მეთოდი. ამის ნაცვლად, ჩვენ ვიყენებთ "MoveTo(…)". Here is a minimal bit of code to accomplish this:

 საჯარო override ცნოს ItemAdded(SPItemEventProperties თვისებები)
        {
            SPFile f = properties.ListItem.File;

            f.MoveTo(properties.ListItem.ParentList.RootFolder.Url + "/xyzzy.doc");
            f.Update();

        }

The only tricky bit is the "properties.ListItem.ParentList.RootFolder.Url". The MoveTo() method requires a URL. That mashed up string points me to the root folder of my current document library. This allows me to avoid any hard coding in my event receiver.

ეს არის უფრო სასარგებლო ვერსია, რომ იგივეს შვება, but assigns the name of the file to "Title":

 საჯარო override ცნოს ItemAdded(SPItemEventProperties თვისებები)
        {
            DisableEventFiring();

            // მიანიჭეთ ტიტული ამ ელემენტის სახელით ფაილი თვით.
 // შენიშვნა: ეს დავალება უნდა მოხდეს, სანამ ცვლილებები ფაილი თვით.
 // მოვუწოდებთ განახლება() on SPFile ჩანს გაუქმების ქონების
 // გარკვეული.  Updates to "Title" ვერ მოხერხდა, სანამ, რომ ცვლილება (და განახლება() მოვუწოდებთ)
 // იყო გადაინაცვლა ცვლილება ფაილი სახელი.
            properties.ListItem["Title"] = Properties.ListItem.File.Name;

            properties.ListItem.Update();

            SPFile f = properties.ListItem.File;

            // მიიღეთ გაგრძელების ფაილი.  ჩვენ გვჭირდება, რომ მოგვიანებით.
 სიმებიანი spfileExt = ახალი ფაილის სტატისტიკა(f.Name).გაფართოება;

            // Rename the file to the list item's ID and use the file extension to keep
 // that part of it intact.
            f.MoveTo(properties.ListItem.ParentList.RootFolder.Url +
                "/" + properties.ListItem["ID"] + spfileExt);

            // Commit the move.
            f.Update();

            EnableEventFiring();
        }

4 thoughts on "სწრაფი & მარტივი: სახელის დამატებულია ფაილი გამოყენებით SharePoint ობიექტური მოდელის ვია თარიღის მიმღები

  1. პოლ Galvin
    Thanks for pointing that out. I don’t know an answer to that one either … will be interesting to see how to manage it.
    RE: async & redirection
    I wonder if moving it to an ItemAdding will solve it. That’s a big hole.
    მადლობა!
    –პოლ
  2. Nadeem Mitha
    Because the file gets moved asynchronously, there is a possibility of being redirected to EditForm.aspx before the move is committed (და ეს ცუდი). You’ll see that this happens if you make the code sleep before the MoveTo line. I still haven’t figured out what can be done about that problem aside from using a customized edit form.

დატოვე პასუხი უნდა Nadeem Mitha პასუხის გაუქმება

თქვენი ელ-ფოსტა არ გამოქვეყნდება. აუცილებელი ველები მონიშნულია *