Showing posts with label SharePoint. Show all posts
Showing posts with label SharePoint. Show all posts
Friday, December 03, 2010

修改SharePoint網站之清單"建立者"欄位

Environment
Server: Win2003 + SharePoint Service 3

需求: 當透過WSS UI的「儲存清單為範本」+「包括內容」選項將清單匯出後,再匯入時,所有清單項目的「建立者」欄位都會變成匯入人的名字。如果資訊人員佛心來的想替使用者改掉... 是可以做的,寫程式囉。

利用Microsoft提供的SharePoint API: Microsoft.SharePoint.dll。這支dll可以在SharePoint server上的\\Computer_Name\Drive$\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI 內找到。

但是,如果你覺得把他複製出來到自己本機開發那就錯了。這麼做應該會遇到「無法載入檔案或組件或其相依性的其中之一」的錯誤,就像這篇SharePoint Forums上的討論,請直接在裝有SharePoint Service的server上做開發.. 如果有測試機器是再好不過的,就可以參考這篇文章(Can we update the values of “Created By”, "Modified By” columns in SharePoint lists?)直接在Visual Studio上參考上述組件做開發。

如果你正好沒有測試環境,而且也無法在production上安裝VisualStudio這麼龐大的怪物的話,就參考這篇文章(Updating the “Created By” and "Modified By” Columns in SharePoint lists using POWERSHELL)。沒錯! 就是用POWERSHELL。這也是我用的方法。

script:

   1:  # Get the SharePoint Assembly 


   2:  # You will need to run this on the SharePoint Server


   3:  [Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") 


   4:   


   5:  # create a new object called $SPSite


   6:  # Make sure you have the last “/” in the url on the site 


   7:  # allow $SPWeb to be the OpenWeb from the site


   8:  $SPSite = New-Object Microsoft.SharePoint.SPSite("http://server-name/")


   9:  $SPWeb = $SPSite.OpenWeb() 


  10:   


  11:  # Get the list ModifyCreatedBy 


  12:  $SPList = $SPWeb.Lists["ListName"] 


  13:   


  14:  # Get the Collection of the List Items


  15:  $SPListItemCollection = $SPList.Items 


  16:   


  17:  # iterate the Collection


  18:  foreach ($ListItem in $SPListItemCollection) 


  19:  {


  20:   


  21:    # The user in this case was UserA with id of 8 from the SPWeb users 


  22:    $SPFieldUserValue = New-Object Microsoft.SharePoint.SPFieldUserValue($SPWeb,8,"UserA") 


  23:   


  24:    $ListItem["Author"] = $SPFieldUserValue


  25:   


  26:    # Note: Editor will be the account that you are running the Powershell under unless you update Editor as well 


  27:    # ListItem["Editor"] = $SPFieldUserValue


  28:   


  29:    $ListItem.Update() 


  30:  } 


  31:   


  32:  $SPWeb.Update() 


  33:   


  34:  # Still TODO Disposes, Error Checking, Change to take Parameters, etc


  35:   



 
上面的script是改了整個指定清單的建立者欄位,如果你還需要先判斷是那些文件,可以參考一下MSDN Library看看SPListItem還有哪些properities可以用。若是powershell語法不熟的話(尤其他又喜歡跟一般語法同.... 特別是比較運算子...),請參考reference#4, #5,或自行google。



嗯.. 大概就以上資訊,即可作業!!



Ref:




  1. [SharePoint Forums] Could not find Microsoft.SharePoint.Library.dll


  2. [MSDN Blog] Can we update the values of “Created By”, "Modified By” columns in SharePoint lists?


  3. [MSDN Blog] Updating the “Created By” and "Modified By” Columns in SharePoint lists using POWERSHELL


  4. [TechNet 技術文件庫] Running Windows PowerShell Scripts


  5. [賴榮樞的軟體資訊誌] scripting_PowerShell tag


  6. [MSDN Library] Microsoft.SharePoint Namespace

Friday, November 06, 2009

WSS 3.0 Open links In New Window

這兩個禮拜一直在替WSS換長相,摸出了一些心得。不過今天不講這些XD
講一下WSS的超連結,預設行為都是開在同一個頁面,但是對於那些專門做連結的清單、或是就是要另開新視窗的連結而言,若無法改變WSS行為,操作起來就相當麻煩。

Entonces, 該如何設定讓WSS的特定頁面的超連結行為為開新視窗呢?
  1. 移駕至該頁面
  2. 點選右上角"網站動作" > "編輯頁面" > "新增網頁組件"
  3. 選擇"內容編輯器網頁組件" > 新增
  4. 組件新增至頁面上後,點選內容的"開啟工具窗格"
  5. 點選"原始檔編輯器...",會跳出一個空白對話框,在裡面貼上以下程式:

       1:  <script language="JavaScript">

       2:   

       3:  //add an entry to the _spBodyOnLoadFunctionNames array

       4:  //so that our function will run on the pageLoad event

       5:   

       6:  _spBodyOnLoadFunctionNames.push("rewriteLinks");

       7:   

       8:  function rewriteLinks() {

       9:   

      10:       //create an array to store all the anchor elements in the page

      11:       var anchors = document.getElementsByTagName("a");

      12:   

      13:       //loop through the array

      14:   

      15:       for (var x=0; x<anchors.length; x++) {

      16:       //does this anchor element contain #openinnewwindow?

      17:   

      18:            if (anchors[x].outerHTML.indexOf('#openinnewwindow')>0) {             

      19:               //store the HTML for this anchor element

      20:               oldText = anchors[x].outerHTML;

      21:                

      22:               //rewrite the URL to remove our test text and add a target instead

      23:               newText = oldText.replace(/#openinnewwindow/,'" target="_blank');

      24:   

      25:               //write the HTML back to the browser              

      26:               anchors[x].outerHTML = newText;

      27:            }

      28:       }

      29:  }

      30:   

      31:  </script>


  6. 儲存後,另外在"版面配置"區段下將"隱藏"方框勾選起來即可套用,並結束編輯頁面。
  7. 最後,針對該頁面需要在新視窗開啟的超連結網址後面全部加上"#openinnewwindow"即可!!!
稍微說明一下上面那段javascript主要在做什麼:
首先,第7點說的在網址後面加上"#openinnewwindow"字樣其實是跟程式互相配合的,行18即是尋找所有超連結網址裡是否有這個字樣,如果有找到,就把他換成" target=_blank"這樣而已XD 所以有該字樣的 <A>tag行為就會變成以新視窗開啟了~


Thursday, June 11, 2009

SharePoint: Recycle Bin

這兩天稍為研究了一下WSS3.0資源回收桶的運作模式。

以系統預設的狀況來看,可以用下圖來說明:

  1. 使用者從各子網站將任何檔案刪除後,檔案會進入該網站下的資源回收桶。
    此時使用者還可自行從資源回收桶裡回復該檔案。
    資料庫裡與該檔案相關連的資料 [DeleteTransactionId]欄位會被註記為非"0X"的十六進位值。同時在網站集合層級的[使用者資源回收桶項目]下可以看見這個檔案。
  2. 使用者從自己網站下的資源回收桶刪除後,即無法再看見該檔案。
    但並非永久刪除,此時在網站集合層級的[已從使用者資源回收桶項目刪除]的連結下還可看見該檔案,使用者可要求網站管理員幫助回復檔案。
  3. 若檔案在[已從使用者資源回收桶項目刪除]被刪除,就真的從資料庫裡把該筆檔案刪除了。
完畢。
其他細節請看下面參考連結。

Ref:
Wednesday, June 10, 2009

SharePoint: About Deleted Item

重要文件暫記:

Tags