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);
    }
}