SharePoint. Change document icon in document library?

3k views Asked by At

I would like to change document icon in document library at runtime. Icon should be changed based on some condition I have in code. There is no difference what extension of file is (txt, doc, pdf, etc..). Currently I use HTTP module to parse server response and change icon in runtime. However this solution is not reliable as page HTML markup may be changed, different views has different behavior and there are issues with registration HTTP module in SharePoint 2010. Is there any API or any other reliable way to change document's icon at runtime?

I would appreciate any help. Thanks beforehand, -Petro

1

There are 1 answers

0
EvgK On BEST ANSWER

Here is the schema of Sharepoint 2010 standard DocIcon Field (its quite huge):

  <Field ID="{081c6e4c-5c14-4f20-b23e-1a71ceb6a67c}" Type="Computed" ReadOnly="TRUE" Name="DocIcon" DisplayName="Тип" TextOnly="TRUE" ClassInfo="Icon" AuthoringInfo="(значок связи с документом)" SourceID="http://schemas.microsoft.com/sharepoint/v3" StaticName="DocIcon" FromBaseType="TRUE">
  <FieldRefs>
    <FieldRef Name="File_x0020_Type" />
    <FieldRef Name="FSObjType" />
    <FieldRef Name="FileRef" />
    <FieldRef Name="FileLeafRef" />
    <FieldRef Name="HTML_x0020_File_x0020_Type" />
    <FieldRef Name="PermMask" />
    <FieldRef Name="CheckoutUser" ShowField="Title" />
    <FieldRef Name="IsCheckedoutToLocal" />
    <FieldRef Name="ServerUrl" />
    <FieldRef Name="IconOverlay" />
  </FieldRefs>
  <DisplayPattern>
    <SetVar Name="DocIconImg">
      <SetVar Name="DocIconAltText">
        <IfEqual>
          <Expr1>
            <LookupColumn Name="FSObjType" />
          </Expr1>
          <Expr2>1</Expr2>
          <Then>
            <IfSubString>
              <Expr1>0x0120D5</Expr1>
              <Expr2>
                <Column Name="ContentTypeId" />
              </Expr2>
              <Then>
                <HTML>Коллекция документов: </HTML>
                <LookupColumn Name="FileLeafRef" HTMLEncode="TRUE" />
              </Then>
              <Else>
                <HTML>Папка: </HTML>
                <LookupColumn Name="FileLeafRef" HTMLEncode="TRUE" />
              </Else>
            </IfSubString>
          </Then>
          <Else>
            <LookupColumn Name="FileLeafRef" HTMLEncode="TRUE" />
          </Else>
        </IfEqual>
      </SetVar>
      <SetVar Name="DocIconFileName">
        <IfEqual>
          <Expr1>
            <Column Name="IconOverlay" />
          </Expr1>
          <Expr2 />
          <Then>
            <IfEqual>
              <Expr1>
                <LookupColumn Name="FSObjType" />
              </Expr1>
              <Expr2>1</Expr2>
              <Then>
                <IfEqual>
                  <Expr1>
                    <Column Name="HTML_x0020_File_x0020_Type" />
                    <HTML>|</HTML>
                    <Column Name="File_x0020_Type" />
                  </Expr1>
                  <Expr2>
                    <HTML>|</HTML>
                  </Expr2>
                  <Then>
                    <HTML>folder.gif</HTML>
                  </Then>
                  <Else>
                    <SetVar Name="FolderIconFromMap">
                      <MapToIcon>
                        <Column Name="HTML_x0020_File_x0020_Type" />
                        <HTML>|</HTML>
                        <Column Name="File_x0020_Type" />
                      </MapToIcon>
                    </SetVar>
                    <IfEqual>
                      <Expr1>
                        <GetVar Name="FolderIconFromMap" />
                      </Expr1>
                      <Expr2>
                        <MapToIcon />
                      </Expr2>
                      <Then>
                        <HTML>folder.gif</HTML>
                      </Then>
                      <Else>
                        <GetVar Name="FolderIconFromMap" />
                      </Else>
                    </IfEqual>
                  </Else>
                </IfEqual>
              </Then>
              <Else>
                <MapToIcon>
                  <Column Name="HTML_x0020_File_x0020_Type" />
                  <HTML>|</HTML>
                  <Column Name="File_x0020_Type" />
                </MapToIcon>
              </Else>
            </IfEqual>
          </Then>
          <Else>
            <MapToIcon>
              <Column Name="IconOverlay" />
            </MapToIcon>
          </Else>
        </IfEqual>
      </SetVar>
      <HTML><![CDATA[<img border="0" alt="]]></HTML>
      <GetVar Name="DocIconAltText" />
      <HTML><![CDATA[" title="]]></HTML>
      <GetVar Name="DocIconAltText" />
      <HTML><![CDATA[" src="/_layouts/images/]]></HTML>
      <GetVar Name="DocIconFileName" />
      <HTML><![CDATA[" />]]></HTML>
    </SetVar>
    <SetVar Name="DocIconOverlayImg">
      <IfEqual>
        <Expr1>
          <Column Name="IconOverlay" />
        </Expr1>
        <Expr2 />
        <Then>
          <IfEqual>
            <Expr1>
              <Column Name="CheckoutUser" />
            </Expr1>
            <Expr2 />
            <Else>
              <SetVar Name="DocIconOverlayAltText">
                <LookupColumn Name="FileLeafRef" HTMLEncode="TRUE" />
                <HTML><![CDATA[&#10;Кем извлечено: ]]></HTML>
                <LookupColumn Name="CheckoutUser" ShowField="Title" HTMLEncode="TRUE" />
              </SetVar>
              <HTML><![CDATA[<img class="ms-vb-icon-overlay" alt="]]></HTML>
              <GetVar Name="DocIconOverlayAltText" />
              <HTML><![CDATA[" title="]]></HTML>
              <GetVar Name="DocIconOverlayAltText" />
              <HTML><![CDATA[" src="/_layouts/images/checkoutoverlay.gif" />]]></HTML>
            </Else>
          </IfEqual>
        </Then>
        <Else>
          <HTML><![CDATA[<img class="ms-vb-icon-overlay" alt="*" src="/_layouts/images/]]></HTML>
          <MapToOverlay>
            <Column Name="IconOverlay" />
          </MapToOverlay>
          <HTML><![CDATA[" />]]></HTML>
        </Else>
      </IfEqual>
    </SetVar>
    <IfEqual>
      <Expr1>
        <LookupColumn Name="FSObjType" />
      </Expr1>
      <Expr2>1</Expr2>
      <Then>
        <FieldSwitch>
          <Expr>
            <GetVar Name="RecursiveView" />
          </Expr>
          <Case Value="1">
            <GetVar Name="DocIconImg" />
            <GetVar Name="DocIconOverlayImg" />
          </Case>
          <Default>
            <SetVar Name="UnencodedFilterLink">
              <SetVar Name="RootFolder">
                <HTML>/</HTML>
                <LookupColumn Name="FileRef" />
              </SetVar>
              <SetVar Name="SkipHost">1</SetVar>
              <SetVar Name="FolderCTID">
                <FieldSwitch>
                  <Expr>
                    <ListProperty Select="EnableContentTypes" />
                  </Expr>
                  <Case Value="1">
                    <Column Name="ContentTypeId" />
                  </Case>
                </FieldSwitch>
              </SetVar>
              <FilterLink Default="" Paged="FALSE" />
            </SetVar>
            <FieldSwitch>
              <Expr>
                <GetVar Name="FileDialog" />
              </Expr>
              <Case Value="1">
                <GetVar Name="DocIconImg" />
                <GetVar Name="DocIconOverlayImg" />
              </Case>
              <Default>
                <HTML><![CDATA[<a href="]]></HTML>
                <GetVar Name="UnencodedFilterLink" HTMLEncode="TRUE" />
                <HTML><![CDATA[" onmousedown="javascript:VerifyFolderHref(this,event, ']]></HTML>
                <ScriptQuote NotAddingQuote="TRUE">
                  <GetVar Name="UnencodedFilterLink" />
                </ScriptQuote>
                <HTML><![CDATA[',']]></HTML>
                <ScriptQuote NotAddingQuote="TRUE">
                  <ServerProperty Select="HtmlTrProgId">
                    <Column Name="File_x0020_Type" />
                  </ServerProperty>
                </ScriptQuote>
                <HTML><![CDATA[',']]></HTML>
                <ScriptQuote NotAddingQuote="TRUE">
                  <ListProperty Select="DefaultItemOpen" />
                </ScriptQuote>
                <HTML><![CDATA[',']]></HTML>
                <ScriptQuote NotAddingQuote="TRUE">
                  <MapToControl>
                    <Column Name="HTML_x0020_File_x0020_Type" />
                    <HTML>|</HTML>
                    <Column Name="File_x0020_Type" />
                  </MapToControl>
                </ScriptQuote>
                <HTML><![CDATA[',']]></HTML>
                <ScriptQuote NotAddingQuote="TRUE">
                  <Column Name="HTML_x0020_File_x0020_Type" />
                </ScriptQuote>
                <HTML><![CDATA[',']]></HTML>
                <ScriptQuote NotAddingQuote="TRUE">
                  <ServerProperty Select="GetServerFileRedirect">
                    <Field Name="ServerUrl" />
                    <HTML>|</HTML>
                    <Column Name="HTML_x0020_File_x0020_Type" />
                  </ServerProperty>
                </ScriptQuote>
                <HTML><![CDATA[')"]]></HTML>
                <HTML><![CDATA[" onclick="return HandleFolder(this,event, ']]></HTML>
                <ScriptQuote NotAddingQuote="TRUE">
                  <GetVar Name="UnencodedFilterLink" />
                </ScriptQuote>
                <HTML><![CDATA[',']]></HTML>
                <ScriptQuote NotAddingQuote="TRUE">
                  <ServerProperty Select="HtmlTransform" />
                </ScriptQuote>
                <HTML><![CDATA[',']]></HTML>
                <ScriptQuote NotAddingQuote="TRUE">
                  <ServerProperty Select="HtmlTrAcceptType">
                    <Column Name="File_x0020_Type" />
                  </ServerProperty>
                </ScriptQuote>
                <HTML><![CDATA[',']]></HTML>
                <ScriptQuote NotAddingQuote="TRUE">
                  <ServerProperty Select="HtmlTrHandleUrl">
                    <Column Name="File_x0020_Type" />
                  </ServerProperty>
                </ScriptQuote>
                <HTML><![CDATA[',']]></HTML>
                <ScriptQuote NotAddingQuote="TRUE">
                  <ServerProperty Select="HtmlTrProgId">
                    <Column Name="File_x0020_Type" />
                  </ServerProperty>
                </ScriptQuote>
                <HTML><![CDATA[',']]></HTML>
                <ScriptQuote NotAddingQuote="TRUE">
                  <ListProperty Select="DefaultItemOpen" />
                </ScriptQuote>
                <HTML><![CDATA[',']]></HTML>
                <ScriptQuote NotAddingQuote="TRUE">
                  <MapToControl>
                    <Column Name="HTML_x0020_File_x0020_Type" />
                    <HTML>|</HTML>
                    <Column Name="File_x0020_Type" />
                  </MapToControl>
                </ScriptQuote>
                <HTML><![CDATA[',']]></HTML>
                <ScriptQuote NotAddingQuote="TRUE">
                  <Column Name="HTML_x0020_File_x0020_Type" />
                </ScriptQuote>
                <HTML><![CDATA[',']]></HTML>
                <ScriptQuote NotAddingQuote="TRUE">
                  <ServerProperty Select="GetServerFileRedirect">
                    <Field Name="ServerUrl" />
                    <HTML>|</HTML>
                    <Column Name="HTML_x0020_File_x0020_Type" />
                  </ServerProperty>
                </ScriptQuote>
                <HTML><![CDATA[',']]></HTML>
                <ScriptQuote NotAddingQuote="TRUE">
                  <Column Name="CheckoutUser" />
                </ScriptQuote>
                <HTML><![CDATA[',']]></HTML>
                <ScriptQuote NotAddingQuote="TRUE">
                  <UserID AllowAnonymous="TRUE" />
                </ScriptQuote>
                <HTML><![CDATA[',']]></HTML>
                <ScriptQuote NotAddingQuote="TRUE">
                  <ListProperty Select="ForceCheckout" />
                </ScriptQuote>
                <HTML><![CDATA[',']]></HTML>
                <ScriptQuote NotAddingQuote="TRUE">
                  <Field Name="IsCheckedoutToLocal" />
                </ScriptQuote>
                <HTML><![CDATA[',']]></HTML>
                <ScriptQuote NotAddingQuote="TRUE">
                  <Field Name="PermMask" />
                </ScriptQuote>
                <HTML><![CDATA[');">]]></HTML>
                <GetVar Name="DocIconImg" />
                <GetVar Name="DocIconOverlayImg" />
                <HTML><![CDATA[</a>]]></HTML>
              </Default>
            </FieldSwitch>
          </Default>
        </FieldSwitch>
      </Then>
      <Else>
        <HTML><![CDATA[<a href="]]></HTML>
        <Field Name="ServerUrl" URLEncodeAsURL="TRUE" />
        <HTML><![CDATA[" onclick="return DispEx(this,event,']]></HTML>
        <ScriptQuote NotAddingQuote="TRUE">
          <ServerProperty Select="HtmlTransform" />
        </ScriptQuote>
        <HTML><![CDATA[',']]></HTML>
        <ScriptQuote NotAddingQuote="TRUE">
          <ServerProperty Select="HtmlTrAcceptType">
            <Column Name="File_x0020_Type" />
          </ServerProperty>
        </ScriptQuote>
        <HTML><![CDATA[',']]></HTML>
        <ScriptQuote NotAddingQuote="TRUE">
          <ServerProperty Select="HtmlTrHandleUrl">
            <Column Name="File_x0020_Type" />
          </ServerProperty>
        </ScriptQuote>
        <HTML><![CDATA[',']]></HTML>
        <ScriptQuote NotAddingQuote="TRUE">
          <ServerProperty Select="HtmlTrProgId">
            <Column Name="File_x0020_Type" />
          </ServerProperty>
        </ScriptQuote>
        <HTML><![CDATA[',']]></HTML>
        <ScriptQuote NotAddingQuote="TRUE">
          <ListProperty Select="DefaultItemOpen" />
        </ScriptQuote>
        <HTML><![CDATA[',']]></HTML>
        <ScriptQuote NotAddingQuote="TRUE">
          <MapToControl>
            <Column Name="HTML_x0020_File_x0020_Type" />
            <HTML>|</HTML>
            <Column Name="File_x0020_Type" />
          </MapToControl>
        </ScriptQuote>
        <HTML><![CDATA[',']]></HTML>
        <ScriptQuote NotAddingQuote="TRUE">
          <Column Name="HTML_x0020_File_x0020_Type" />
        </ScriptQuote>
        <HTML><![CDATA[',']]></HTML>
        <ScriptQuote NotAddingQuote="TRUE">
          <ServerProperty Select="GetServerFileRedirect">
            <Field Name="ServerUrl" />
            <HTML>|</HTML>
            <Column Name="HTML_x0020_File_x0020_Type" />
          </ServerProperty>
        </ScriptQuote>
        <HTML><![CDATA[',']]></HTML>
        <ScriptQuote NotAddingQuote="TRUE">
          <Column Name="CheckoutUser" />
        </ScriptQuote>
        <HTML><![CDATA[',']]></HTML>
        <ScriptQuote NotAddingQuote="TRUE">
          <UserID AllowAnonymous="TRUE" />
        </ScriptQuote>
        <HTML><![CDATA[',']]></HTML>
        <ScriptQuote NotAddingQuote="TRUE">
          <ListProperty Select="ForceCheckout" />
        </ScriptQuote>
        <HTML><![CDATA[',']]></HTML>
        <ScriptQuote NotAddingQuote="TRUE">
          <Field Name="IsCheckedoutToLocal" />
        </ScriptQuote>
        <HTML><![CDATA[',']]></HTML>
        <ScriptQuote NotAddingQuote="TRUE">
          <Field Name="PermMask" />
        </ScriptQuote>
        <HTML><![CDATA[')">]]></HTML>
        <GetVar Name="DocIconImg" />
        <GetVar Name="DocIconOverlayImg" />
        <HTML><![CDATA[</a>]]></HTML>
      </Else>
    </IfEqual>
  </DisplayPattern>
</Field>

You can get DocIcon field of a library you want to modify icons for (through code) and modify its schema. As you can see there are a lot of XSLT in section, but it's quite simple and new patterns can be added without big problems (and old ones can be removed). Another way is to create your own custom field type and use it instead of standard one to display an icon.