niedziela, 21 czerwca 2015

Create SendTo connection programmatically

private const string CONTENT_ORGANIZER_URL = "/_vti_bin/officialfile.asmx";

/// To run this code without Access Denied error, RemoteAdministratorAccessDenied must be set False. Do it with PowerShell:
/// $contentService = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
/// $contentService.RemoteAdministratorAccessDenied = $false
/// $contentService.Update()
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWebApplication webapp = newRC.WebApplication;
       var newhost = new SPOfficialFileHost(true);
       newhost.OfficialFileUrl = new Uri("url of site with content organizer feature on" + CONTENT_ORGANIZER_URL);
       newhost.OfficialFileName = "Your connection name";
       newhost.Explanation = "Your connection description";
       newhost.ShowOnSendToMenu = false;
       newhost.Action = SPOfficialFileAction.Move;
webapp.OfficialFileHosts.Add(newhost);
webapp.Update();
});


Code snippet adding javascript that maximizes dialog window

if (SPContext.Current.IsPopUI)
{
   if (!Page.ClientScript.IsClientScriptBlockRegistered(GetType(), "ScriptWinMax"))
   {
      string script = @"function _maximizeWindow() {
         var currentDialog = SP.UI.ModalDialog.get_childDialog();
         if (currentDialog != null && !currentDialog.$S_0)
            currentDialog.$z();
         }       
         ExecuteOrDelayUntilScriptLoaded(_maximizeWindow, 'sp.ui.dialog.js');";

      Page.ClientScript.RegisterClientScriptBlock(GetType(), "ScriptWinMax", script, true);
   }
}

jQuery 'Hello world' snippet


<script type="text/javascript">
   $(document).ready(function () {
      // simple test jquery is here
      $("#hello").html("Hello World by JQuery");
      });
   });
</script>

<div id="hello"></div>

Code sample of using ListViewWebPart

Application Page:

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
   <asp:UpdatePanel ID="panelLV" runat="server" UpdateMode="Conditional"></asp:UpdatePanel>
</asp:Content>

Code behind:

protected void Page_Load(object sender, EventArgs e)
{
   if (!IsPostBack)
   { 
      createLVWP();
   }
}

private void createLVWP()
{
   SPWeb web = SPContext.Current.Web;
   SPList list = web.GetList(
         SPUrlUtility.CombineUrl(web.ServerRelativeUrl, myListUrl));

   lvwp = new ListViewWebPart();
   lvwp.ListName = list.ID.ToString("B").ToUpper();
   lvwp.ViewGuid = list.DefaultView.ID.ToString("B").ToUpper();
   lvwp.ChromeType = PartChromeType.None;
   
   filterLVWP();
   this.panelLV.ContentTemplateContainer.Controls.Add(lvwp);
}

private void filterLVWP()
{
   string query = string.Format(
@"<Contains>
<FieldRef Name='myFieldName' /><Value Type='Text'>{0}</Value>
</Contains>", myFieldValue);

   XmlDocument doc = new XmlDocument();
   doc.LoadXml(lvwp.ListViewXml);
   XmlNode queryNode = doc.SelectSingleNode("//Query");
   XmlNode whereNode = queryNode.SelectSingleNode("Where");
   if (whereNode != null)
      queryNode.RemoveChild(whereNode);

   XmlNode newNode = doc.CreateNode(XmlNodeType.Element, "Where", String.Empty);
   newNode.InnerXml = query.ToString();
   queryNode.AppendChild(newNode);
   lvwp.ListViewXml = doc.OuterXml;
}

How to use UpdatePanel with LVWP in SharePoint 2010 WebPart?

This is example of classic Web Part (i.e. not Visual Web Part) with UpdatePanel and ListViewWebPart put on it.

First assure that you have this using statement:
using System.Web.UI;
and you reference in your project System.Web.Extensions library. Otherwise you won’t be able to use UpdatePanel control.
Here is the sample WebPart class:

[ToolboxItemAttribute(false)]
public class MyWP : System.Web.UI.WebControls.WebParts.WebPart
{
    TextBox txtExample1;
    TextBox txtExample1;
    ListViewWebPart lvwp;
    SPList list;
    UpdatePanel panelMain;
   
    protected override void CreateChildControls()
    {
        panelMain = new UpdatePanel();
        panelMain.UpdateMode = UpdatePanelUpdateMode.Conditional;

        txtExample1 = new TextBox();
        txtExample2 = new TextBox();

        list = SPContext.Current.Web.GetList(SPUrlUtility.CombineUrl(SPContext.Current.Web.Url, "Lists/MyList"));
        lvwp = new ListViewWebPart();
        lvwp.ListName = list.ID.ToString("B").ToUpper();
        lvwp.ViewGuid = list.DefaultView.ID.ToString("B").ToUpper();
        lvwp.ChromeType = PartChromeType.None;

        Controls.Add(new LiteralControl(" Label1: "));
        Controls.Add(txtExample1);
        Controls.Add(new LiteralControl(" Label2: "));
        Controls.Add(txtExample1);
        panelMain.ContentTemplateContainer.Controls.Add(lvwp);
        this.Controls.Add(panelMain);
    }
}



czwartek, 10 kwietnia 2014

Visual Studion: Install to GAC on Post Build event

In Visual Studio 2012:
"$(FrameworkSDKDir)bin\NETFX 4.0 Tools\gacutil.exe" /i "$(TargetPath)" /f

In Visual Studio 2010:
"$(FrameworkSDKDir)bin\gacutil.exe" /i "$(TargetPath)" /f

Change rendering of the List using XSL

I have a list definition in Visual Studio. Now I want to modify the rendering of the list views using XSL.
First I point my custom xsl file in the XslLink attribute in the list Schema.xml file:

<XslLink Default="TRUE">CustomLibrary.xsl</XslLink>

MailType is a custom column of type Choice and has the following options to choose: In, Out, Other.
I’d like to change two things:
  •  the way its values are displayed in the ListView: show icons instead of text
  • the column header of the ListView: instead of the default DisplayName, I want custom text “In/Out”


Here is the XSL file:

<xsl:stylesheet
       xmlns:x="http://www.w3.org/2001/XMLSchema"
       xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" version="1.0" exclude-result-prefixes="xsl msxsl ddwrt"
       xmlns:asp="http://schemas.microsoft.com/ASPNET/20"
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
       xmlns:msxsl="urn:schemas-microsoft-com:xslt"
       xmlns:SharePoint="Microsoft.SharePoint.WebControls"
       xmlns:ddwrt2="urn:frontpage:internal"
       xmlns:o="urn:schemas-microsoft-com:office:office">

<xsl:include href="/_layouts/xsl/main.xsl"/>
       <xsl:include href="/_layouts/xsl/internal.xsl"/>

       <xsl:template name="FieldRef_header.MailType" match="FieldRef[@Name='MailType']" mode="header">
             <th nowrap="nowrap" scope="col" onmouseover="OnChildColumn(this)">
                    <xsl:attribute name="class">ms-vh2</xsl:attribute>
                    <xsl:call-template name="dvt_headerfield">
                           <xsl:with-param name="fieldname">
                                  <xsl:value-of select="@Name"/>
                           </xsl:with-param>
                           <xsl:with-param name="fieldtitle">
                                  <xsl:value-of select="'In/Out'"/>
                           </xsl:with-param>
                           <xsl:with-param name="displayname">
                                  <xsl:value-of select="@DisplayName"/>
                           </xsl:with-param>
                           <xsl:with-param name="fieldtype">x:string</xsl:with-param>
                    </xsl:call-template>
             </th>
       </xsl:template>


<xsl:template name="FieldRef_body.MailType" match="FieldRef[@Name='MailType']" mode="body">
             <xsl:param name="thisNode" select="."/>
             <xsl:choose>
                    <xsl:when test="$thisNode/@*[name()=current()/@Name] = 'In'">
                           <img src="/_layouts/images/gbwwain.png" alt="Typ: {$thisNode/@MailType}" title="{$thisNode/@MailType}" />
                    </xsl:when>
                    <xsl:when test="$thisNode/@*[name()=current()/@Name] = 'Out'">
                           <img src="/_layouts/images/gbwwaoof.png" alt="Typ: {$thisNode/@MailType}" title="{$thisNode/@MailType}" />
                    </xsl:when>
                    <xsl:when test="$thisNode/@*[name()=current()/@Name] = 'Other'">
                           <img src="/_layouts/images/generaldocument.gif" alt="Typ: {$thisNode/@MailType}" title="{$thisNode/@MailType}" />
                    </xsl:when>
                    <xsl:otherwise>
                           <span></span>
                    </xsl:otherwise>
             </xsl:choose>
       </xsl:template>
</xsl:stylesheet>

The above file needs to be deployed to the {SharePointRoot}\Template\Layouts\XSL\ folder.