顯示具有 SharePoint 標籤的文章。 顯示所有文章
顯示具有 SharePoint 標籤的文章。 顯示所有文章

2011年12月17日 星期六

自訂 SharePoint Web Part 功能表

1. WebPart 的下拉功能表也能自訂功能

2. 撰寫以下程式碼

public override WebPartVerbCollection Verbs
{
get
{
WebPartVerb v1 = new WebPartVerb("verb1",
new WebPartEventHandler(Verb1Event));
v1.Text = "自訂功能";
v1.Description = "do something";

return new WebPartVerbCollection(new WebPartVerb[] { v1 });
}
}


void Verb1Event(object sender, WebPartEventArgs e)
{
EnsureChildControls();
txt.Text = "Click 自訂功能!!";
}

客製化 SharePoint Web Part 屬性

1. WebPart 的 Edit Zone 有許多預設的屬性,我們也可以自訂自己想要的屬性

2. 撰寫以下的程式碼,在 Web Part 的屬性上加上 Personalizable 和 WebBrowsable 兩個屬性
[Personalizable(PersonalizationScope.Shared)]
[WebBrowsable(true)]
[Category("自訂屬性")]
[WebDisplayName("顯示文字")]
[Description("")]
public string DisplayText { get; set; }

開發 SharePoint WebPart 連線

1. 建立一個空的 SharePoint 專案

2. 選擇陣列式方案


3. 新增一個 WebPart



4. 在畫面上放上一個TextBox跟一個Button,撰寫以下的程式碼

[ToolboxItemAttribute(false)]
    public class WebPart1 : WebPart
    {
        TextBox txt;
        Button btn;
        protected override void CreateChildControls()
        {
            txt = new TextBox();
            btn = new Button();

            btn.Text = "Click";

            this.Controls.Add(txt);
            this.Controls.Add(btn);
        }
    }

5. 建立另一個 WebPart2 ,來接收 WebPart1 的資料


6. 在 WebPart2 放入一個 Label ,準備接收 WebPart1 的 TextBox傳過來的值

Label lbl;
        protected override void CreateChildControls()
        {
            lbl = new Label();

            this.Controls.Add(lbl);
        }


7. 接下來要讓兩個 WebPart 可以做 Connection,主要是要建立一個 Interface ,兩者透過這個 Inteface 來傳送資料

8. 首先新增一個介面


9. 在 Interface 中撰寫以下程式碼

public interface IData
{
    string Data { get; }
}


10. 定義提供者( WebPart1 ),將提供者實作 IData 這個 Interface,並且要實作 IData 的屬性
將 WebPart 的程式碼修改如下
[ToolboxItemAttribute(false)]
    public class WebPart1 : WebPart,IData
    {
        TextBox txt;
        Button btn;
        protected override void CreateChildControls()
        {
            txt = new TextBox();
            btn = new Button();

            btn.Text = "Click";

            this.Controls.Add(txt);
            this.Controls.Add(btn);
        }

        public string Data
        {
            get
            {
                EnsureChildControls();
                return txt.Text;
            }
        }

    }


11. 提供者( WebPart1 )要加入 ProviderInterface的方法回傳介面,並且加上 ConnectionProvider的屬性
在 WebPart1 加上以下程式碼

[ConnectionProvider("文字資料")]
public IData ProvideInterface()
{
return this;
}


12. 最後再消費者( WebPart2 )加上屬性來接收資料

[ConnectionConsumer("文字資料")]
public void GetInterface(IData myInterface)
{
EnsureChildControls();
lbl.Text = myInterface.Data;
}

13. 將專案佈署到網頁上

14. 設定 WebPart 之間的連線

15. 結果



2011年11月22日 星期二

利用PowerShell 開啟 / 關閉 SharePoint Dashboard 功能


開啟DashBoard (右上角可控制)

$service = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
$addsetting =$service.DeveloperDashboardSettings
$addsetting.DisplayLevel = [Microsoft.SharePoint.Administration.SPDeveloperDashboardLevel]::OnDemand
$addsetting.Update()

開啟DashBoard 
$service = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
$addsetting =$service.DeveloperDashboardSettings
$addsetting.DisplayLevel = [Microsoft.SharePoint.Administration.SPDeveloperDashboardLevel]::On
$addsetting.Update()


關閉
DashBoard

$service = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
$addsetting =$service.DeveloperDashboardSettings
$addsetting.DisplayLevel = [Microsoft.SharePoint.Administration.SPDeveloperDashboardLevel]::Off
$addsetting.Update()
 

2011年10月1日 星期六

利用 PowerShell 停止 SharePoint 上的服務

1. 「以系統管理員身分執行」 啟動 SharePoint 2010 Management Shell

2. 輸入「Get-SPServiceInstance」,並且找到跟服務的 Id

3.  Stop-SPServiceInstance 01c3032e-b4c3-41a5-bf7b-2b789a2d7e8c



2011年9月27日 星期二

SharePoint 使用 Designer 加入 script 的 onload 事件

<script type="text/javascript">
_spBodyOnLoadFunctionNames.push("displayImage");
function displayImage() {
alert("PageLoad...");
}
</script>

2011年9月23日 星期五

修改 SharePoint 的標題和圖片

1. 建立空的 SharePoint 專案

2. 指定為「陣列方案」

3. 建立新的 Feature,更名為「Main」,並且把範圍變更為「Site」層級

4. 加入 SharePoint 對應的影像資料夾,這樣可以很方便的將圖片放到 SharePoint 正確的資料夾目錄下,他的實體路徑會在「C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\IMAGES\<專案名稱>」

5. 接著來寫一支程式,當啟用剛剛那個 Feature 時,會變更網站的 logo

6. 在 Feature 上按右鍵,「加入事件接收器」

7. 




// 取消註解下列方法,以處理功能已啟動後所引發的事件。
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite siteCollection = properties.Feature.Parent as SPSite;
if (siteCollection != null)
{
// save top site's original Title and SiteLogoUrl
SPWeb site = siteCollection.RootWeb;
site.Properties["OriginalTitle"] = site.Title;
site.Properties.Update();
// update the Title and SiteIconUrl
site.Title = "Title 被啟動了";
site.SiteLogoUrl = "_layouts/images/SPFeature/logo.jpg";
site.Update();
}
}


// 取消註解下列方法,以處理功能停用時所引發的事件。
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPSite siteCollection = properties.Feature.Parent as SPSite;
if (siteCollection != null)
{
// restore top site's original Title and SiteLogoUrl
SPWeb site = siteCollection.RootWeb;
//site.Title = site.Properties["OriginalTitle"];
site.Title = "Title 被停用了";
site.SiteLogoUrl = string.Empty;
site.Update();
}
}


8. 建置佈署之後,在 SharePoint 管理中心,進入 Site collection features就可以看到剛剛那個 future被啟動了




9. 回到網站首頁,可以看到圖片跟標題都被修改了



利用 C# 取得 SharePoint 網站集合的資料

1. 建立一個 C# 應用程式,
    NET Framework要用3.5
    要選擇 Any CPU
    要加入參考「Microsoft.SharePoint」


2. 輸入以下的程式碼

string targetSiteUrl = "";
using (SPSite siteCollection = new SPSite(targetSiteUrl))
{
SPWeb site = siteCollection.RootWeb;
MessageBox.Show(site.Title);

}




利用 C# 開啟 SharePoint 的 DashBoard 的功能

1. 開啟 Visual Studio 2010 ,建立一個 Windows Form 應用程式

2. 在畫面上佈置控制項如下

3. 要加入參考,「Microsoft.SharePoint」

4. 要選 NET Framework 3.5、平台要選 Any CPU


5. 貼上以下的程式碼

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint.Administration;

namespace SPSDashBoard
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SPWebService contentService = SPWebService.ContentService;
            SPDeveloperDashboardSettings developerDashboard =
              contentService.DeveloperDashboardSettings;
            developerDashboard.DisplayLevel = SPDeveloperDashboardLevel.On; //關閉是Off
            developerDashboard.Update();
            MessageBox.Show("Developer Dashboard updated.");

        }
    }
}


6. 回到網頁上,可以看到下方已經開啟 DashBoard了


讓 Windows PowerShell 也能使用 Sharepoint 指令

1.  SharePoint 2010 可以用 PowerShell 的指令來操作
     例如我想要取得 SharePoint 的所有 Farm ,可以下以下指令 「GET-SPFarm」


2. 其實在 Windows 2008 也有 PowerShell ,但是如果我在 Windows 2008 的 PowerShell 下 SharePoint 的相關指令,卻會出現錯誤


3. 但是卻可以利用 import SharePoint 的語法,來操作 SharePoint 喔

Add-PSSnapin Microsoft.Sharepoint.PowerShell



2011年9月10日 星期六

SharePoint 上 Ribbon 的 Javascript 語法收集

請先參之前的文章 使用Elements.xml 客製化 SharePoint 中的 Ribbon ( 自定Tab ) CommandAction="javascript:
var notificationId = SP.UI.Notify.addNotification('大家好!!!');




var statusId = SP.UI.Status.addStatus('Quite important status message');
latestId = statusId;
enableRemoveStatusButton();


最後在 </Elements> 前面加上 enableRemoveStatusButton()  這段程式碼

<CustomAction Id="NewRibbonButtonCommand.CheckEnable" Location="ScriptLink"
            ScriptBlock="
                var latestId = '';
                       
                function enableRemoveStatusButton()
                {
                  if (latestId == '')
                  {
                    return false;
                  }
                  else
                  {
                    return true;
                  }
                }"
                />


底色變成紅色

var statusId = SP.UI.Status.addStatus('Very important status message');
SP.UI.Status.setStatusPriColor(statusId, 'red');
latestId = statusId;
enableRemoveStatusButton();



清除某個Id的訊息

SP.UI.Status.removeStatus(latestId);
 latestId = '';
enableRemoveStatusButton();"


清除所有訊息


SP.UI.Status.removeAllStatus(true);
 latestId = '';
enableRemoveStatusButton();"

參考網址:http://www.sharepointnutsandbolts.com/2010/01/customizing-ribbon-part-1-creating-tabs.html

使用Elements.xml 客製化 SharePoint 中的 Ribbon ( Library )


1. 建立一個空的 SharePoint 專案

2. 建立為「沙箱式」方案

3. 加入> 新增項目 > 空元素


<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction
    Id="Ribbon.Library.Actions.AddAButton"
    Location="CommandUI.Ribbon"
    RegistrationId="101"
    RegistrationType="List"
    Title="Add a Ribbon Button">
    <CommandUIExtension>
      <CommandUIDefinitions>
        <CommandUIDefinition
          Location="Ribbon.Library.Share.Controls._children">
          <Button Id="Ribbon.Library.Share.NewRibbonButton"
            Command="NewRibbonButtonCommand"
            LabelText="Hello World"
            TemplateAlias="o2" />
        </CommandUIDefinition>
      </CommandUIDefinitions>
      <CommandUIHandlers>
        <CommandUIHandler
          Command="NewRibbonButtonCommand"
          CommandAction="javascript:alert('Hello, world');" />
      </CommandUIHandlers>
    </CommandUIExtension>
  </CustomAction>
</Elements>


4.  回到網站,Ribbon 的 Library,會多一個 Hello World 的 Button


SharePoint 上 Ribbon 的元件


  1. Tab
  2. Contextual tab group
  3. Group
  4. Control 

使用Elements.xml 客製化 SharePoint 中的 Ribbon ( 自定Tab )

1. 建立一個空的 SharePoint 專案

2. 建立為「沙箱式」方案

3. 加入> 新增項目 > 空元素

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction
    Id="MyCustomRibbonTab"
    Location="CommandUI.Ribbon.ListView"
    RegistrationId="101"
    RegistrationType="List">
    <CommandUIExtension>
      <CommandUIDefinitions>
        <CommandUIDefinition
          Location="Ribbon.Tabs._children">
          <Tab
            Id="Ribbon.CustomTabExample"
            Title="客製化頁籤"
            Description="這是客製化的頁籤!!!"
            Sequence="501">
            <Scaling
              Id="Ribbon.CustomTabExample.Scaling">
              <MaxSize
                Id="Ribbon.CustomTabExample.MaxSize"
                GroupId="Ribbon.CustomTabExample.CustomGroupExample"
                Size="OneLargeTwoMedium"/>
              <Scale
                Id="Ribbon.CustomTabExample.Scaling.CustomTabScaling"
                GroupId="Ribbon.CustomTabExample.CustomGroupExample"
                Size="OneLargeTwoMedium" />
            </Scaling>
            <Groups Id="Ribbon.CustomTabExample.Groups">
              <Group
                Id="Ribbon.CustomTabExample.CustomGroupExample"
                Description="This is a custom group!"
                Title="底下文字"
                Sequence="52"
                Template="Ribbon.Templates.myExample">
                <Controls Id="Ribbon.CustomTabExample.CustomGroupExample.Controls">
                  <Button
                    Id="Ribbon.CustomTabExample.CustomGroupExample.Button1"
                    Command="CustomTabExample.Button1Command"
                    Sequence="15"
                    Description="按鈕一"
                    LabelText="按鈕一號"
                    TemplateAlias="cust1"/>
               
                </Controls>
              </Group>
            </Groups>
          </Tab>
        </CommandUIDefinition>
        <CommandUIDefinition Location="Ribbon.Templates._children">
          <GroupTemplate Id="Ribbon.Templates.myExample">
            <Layout
              Title="OneLargeTwoMedium"
              LayoutTitle="OneLargeTwoMedium">
              <Section Alignment="Top" Type="OneRow">
                <Row>
                  <ControlRef DisplayMode="Large" TemplateAlias="cust1" />
                </Row>
              </Section>
         
            </Layout>
          </GroupTemplate>
        </CommandUIDefinition>
      </CommandUIDefinitions>
      <CommandUIHandlers>
        <CommandUIHandler
          Command="CustomTabExample.Button1Command"
          CommandAction="javascript:
        var dialogOptions = {
          url: '/_layouts/SPSRibbon/Demo.aspx',
          title: 'Demo測試頁',
          allowMaximize: true,
          showClose: true,
          width:500,
          height:400
        };
        SP.UI.ModalDialog.showModalDialog(dialogOptions); " />
   
      </CommandUIHandlers>
    </CommandUIExtension>
  </CustomAction>
</Elements>


4. 因為上面的 javascript 按鈕去讀取一個「_layouts/SPSRibbon/Demo.aspx」的應用程式頁面
    所以現在要產生一個頁面,這個頁面要放在「_layouts/SPSRibbon/」,在SharePoint 2010的作法如下:

    加入 > SharePoint 配置對應資料夾



5. Site Actions > Manage site features 

6. 裡面應該會有一個 future 已經被啟動了

7. 回到網站上,Ribbon 會多了一個 Tab,裡面有一個按鈕





2011年9月8日 星期四

在 SharePoint 中變更主版頁面 Master Page

1. 建立一個網站集合,取名「Demo」,在下面建立一個網頁「test」
http://<-Url>/sites/Demo/Pages/test.aspx

2. 打開 Designer ,切到主版頁面,複製 v4.master ,產生一個新的 master,取名「v5.master」


3. 編輯 v5.master,改一下他的 style,如改成下圖

4. 回到主版頁面,按右鍵,存回,並發佈為主要版本


5. 然後做核准,主版頁面才能使用



6. 接著回到 Designer ,按右鍵,「設為預設主版頁面」

7. 接著回到網頁看結果,已經變成新的樣板了


8. 在網站集合的管理中心,Look And Feel > Master Page

9. 裡面有兩種類型
     Site Master Page :The site master page will be used by all publishing pages
     System Master Page :Use the system master page for all forms and view pages in this site


     之前試了很久,為何設了 Site Master Page 的 master page,我的網站樣式都沒改變,一定要改變 System Master Page 才會變呢?
    今天終於想通了,原來 Site Master Page 改的是 Page,當我建立一個網站集合時,他會建立一個預設的 default.aspx ,這應該不能算是  publishing pages,所以要在網站下面自己再建立網頁,這些新建立的網頁才適用 Site Master Page 的主版頁面
    反之,網站集合以及管理頁面,則適用 System Master Page 的主版頁面