Tuesday, March 17, 2015

Unable to remove a field from a SharePoint list via UI

Hi,

In some cases (such as the "Page Content" field found under the Page Layout columns) it is not possible to delete a field from your list once you have added it. This is because by default some fields have their property set to Sealed="TRUE". Thus, you will not be able to delete the field via the UI, forcing you to use a coding solution. This can be achieved via the following PowerShell script:

Clear-Host
$web = Get-SPWeb http://yourweburl
$list = $web.Lists["YourListName"]
$field = $list.Fields["Page Content"]
$field.AllowDeletion = $true
$field.Sealed = $false
$list.Update()
$field.Delete()
$list.Update()
$web.Dispose()

Friday, February 20, 2015

SQL snippet which shows the progression of a database shrinkage operation



SELECT
    Percent_Complete [Completed],
    Start_time [Start Time],
    Status,
    Command,
    ((estimated_completion_time/60000)) [ETA (Min)],
    ((cpu_time/60000)) [CPU Time (MIN)],
    ((total_elapsed_time/60000)) [Total Elapsed Time (MIN)]
 FROM
    sys.dm_exec_requests 
WHERE
    command = 'DbccFilesCompact'

Wednesday, January 7, 2015

Full Postback Problem With AJAX in SharePoint 2013 Visual WebPart

Hi,

For months I have been battling a postback problem occurring in a visual webpart. I have a bunch of controls (dropdowns, textboxes) which are selected by the user to specify search criteria. If the user then hits the search button, a grid will be refreshed based on the selected parameters. The controls are AJAXified via RadAjaxManager, so that the calls are made asynchronous, preventing unnecessary full page postbacks. Just to go on a sidetrack here: if you encounter your SharePoint webpart doing a first full postback and then functioning fine with the controls AJAXified, you might wanna check how you set things up. The way to go is by dynamically adding an instance of RadAjaxManager and the AjaxSettings to the webpart (NOT the WebPartUserControl!). You can find more information on how to do that right here: http://www.telerik.com/forums/telerik-ajax-issue-with-sharepoint-2010. If you are still struggling, feel free to contact me and I'll try to help you out.

Ok. This seemed to work fine until I would leave the page idle for about two minutes. If the user would then trigger any asynchronous callback, the page would cause a full postback, consequently discarding all search parameters specified. The weird thing is that in IE this happened intermittently, maybe just once in every 500 requests. For Chrome/FireFox it would always happen after about 120 seconds.

I have been digging into the Distributed Cache service and doing all sorts of funky tweaking such as changing the Logon and ViewState cache request timeouts but it did not help a bit. Even after re-installing and even disabling the DCS (which tremendously sped up the page request time btw) the problem persisted.

In October, someone created a ticket with Microsoft for this issue and they stated that it had been fixed in the December CU (http://support2.microsoft.com/kb/2910943)

Now it appears that this hotfix is for foundation (thus not for Server, which we are using).

The only and only working solution for now is to disable Forms Authentication in IIS for the web application where you are using your visual webpart. I have done that just now and solved the problem. Beware that doing this might cause trouble if that web application is being used as a portal.

I hope this helps you for now. Keep an eye out for the Server CU.

-Gary

Thursday, October 16, 2014

How to close a layouts page loaded in an SPModalDialog from server side

ASP:
// In _layouts\YourSolution\dialogpage.aspx:

<asp:Button ID="btnSaveAndClose" runat="server" OnClick="btnSaveAndClose_Click" />

C#:


protected void btnSaveAndClose_Click(object sender, EventArgs e)
{
    // code logic before closing the dialog
    // ..
    // ..
    
    string script = @"window.frameElement.commonModalDialogClose(1, '');";
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Close", script, true);
    
    // alternatives for the script variable are:
    // string script = @"window.frameElement.commitPopup();";
    // string script = @"window.frameElement.cancelPopUp();";

    // NOTE: use RegisterStartupScript when executed from .ASCX/webpart:  
    // ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Close", script, true);
}

A small gotcha I ran into is that I had the btnSaveAndClose control Ajaxified, which caused it to throw exceptions such as:

"0x800a1391 - JavaScript runtime error: 'Sys' is undefined"

and

"0x800a1391 - JavaScript runtime error: 'ULSSendExceptionImpl' is undefined"

Thursday, May 15, 2014

RadGrid duplicate insert after page refresh

Hi guys,

Here's a quick one. I had a problem with a RadGrid I created for an application page the other day. It's a common known problem in the ASP.NET community and I saw several solutions but there is a very simple one to it. Let me explain the issue first. Whenever I would insert/update/delete a record using the grid and I would refresh the page, the application would fire that event again. This could potentially cause duplicate inserts or exceptions being thrown whenever trying to delete a record that does not exist anymore.

It is a classic problem.

The easiest way to get passed this hurdle is to do a redirect after the post. It is up to you to decide whether to redirect the users to, say, a confirmation/thank you page or to the same page.

As for the latter, add the following at the end of your grid event logics:



     Response.Redirect(Request.Url.AbsoluteUri);



After your updates have been made, the page will redirect to itself and prevent the event from firing again.

Tuesday, April 29, 2014

Adding, updating and deleting SPListItems using the SharePoint list webservice in combination with XDocument

Quick code snippet which does the following:

- generates a Batch element of type XDocument (i choose for XDocument rather than XmlDocument because it makes it so much simpler to build up the document
- converts the XDocument object to an XmlDocument using an XmlReader



XmlDocument doc;

XDocument xDoc = new XDocument(
    new XElement("Batch", new XAttribute("OnError", "Continue"),

        // Add new item
        new XElement("Method", new XAttribute("ID", "1"), new XAttribute("Cmd", "New"),
            new XElement("Field", "test0", new XAttribute("Name", "Title")),
            new XElement("Field", "Update", new XAttribute("Name", "UserAction")),
            new XElement("Field", "(GAR-2313)", new XAttribute("Name", "OldValue")),
            new XElement("Field", "(SWX-9582)", new XAttribute("Name", "NewValue"))
        ),

        // Updates item: set column 'ProjectStatus' with value 'CLOSED' for item with ID 4
        new XElement("Method", new XAttribute("ID", "2"), new XAttribute("Cmd", "Update"),
            new XElement("Field", "4", new XAttribute("Name", "ID")),
            new XElement("Field", "CLOSED", new XAttribute("Name", "ProjectStatus"))
        ),

        // Delete items with ID 11 and 17 
        new XElement("Method", new XAttribute("ID", "3"), new XAttribute("Cmd", "Delete"),
            new XElement("Field", "11", new XAttribute("Name", "ID"))),
        new XElement("Method", new XAttribute("ID", "4"), new XAttribute("Cmd", "Delete"),
            new XElement("Field", "11", new XAttribute("Name", "ID")
            )
        )
    )
);

using (XmlReader xmlReader = xDoc.CreateReader())
{
    doc = new XmlDocument();
    doc.Load(xmlReader);
}

// assuming a web reference called 'ListWebService' is present..
using (ListWebService.Lists listsWebSvc = new ListWebService.Lists())
{
    //This passes the default user credentials of the user to the web service system
    listsWebSvc.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

    // replace '_listWebSvcUrl' with the url of your list
    listsWebSvc.Url = _listWebSvcUrl;

    // replace '_listName' with the name of your list
    listsWebSvc.UpdateListItems(_listName, doc); 
}

Sunday, April 13, 2014

Quick reference guide for formatting divs as tables



<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
    <head>
        <style type="text/css">
            .containerButtons {
                width: 1200px;
                height: 40px;
                border-color: orange;
                border-style: solid;
                position: block;
            }
            .divButtonSection {
                width: 15%;
                height: 70%;
                border-color: blue;
                border-style: solid;
                float: right;
                position: inline-block;
                display: table-cell;
                vertical-align: middle;
            }
            .containerInformation {
                width: 1200px;
                height: 200px;
                border-color: grey;
                border-style: solid;
                display: block;
            }
            .divInformationTable {
                width: 24%;
                height: 95%;
                position: relative;
                border-color: green;                
                border-style: dashed;
                display: inline-block;
                margin: 0px 0px 0px 0px;
            }            
            .divInformationRow {
                width: 90%;
                height: 22%;
                position: relative;
                border-color: red;
                border-style: solid;
                display: block;
                margin: 0px 0px 0px 0px;
            }
            .divInformationHeaderCell {
                width: 96%;
                height: 90%;
                position: relative;
                border-color: black;
                border-style: solid;
                display: table-cell;
                margin: 0px 0px 0px 0px;
                float: left;            
            }
            .divInformationCellLabel {
                width: 20%;
                height: 90%;
                position: relative;
                border-color: black;                
                border-style: solid;
                display: table-cell;
                margin: 0px 0px 0px 0px;
                float: left;
            }
            .divInformationCellControl {
                width: 75%;
                height: 90%;
                position: relative;
                border-color: blue;
                border-style: solid;
                display: table-cell;
                margin: 0px 0px 0px 0px;
                float: left;
            }
        </style>
    </head>
    <body>
        <div class="containerInformation">
            <div class="divInformationTable">
                <div class="divInformationRow">
                    <div class="divInformationHeaderCell">
                        Dealer Information
                    </div>
                </div>
                <div class="divInformationRow">
                    <div class="divInformationCellLabel">
                        Label1
                    </div>
                    <div class="divCellControl">
                    Control1
                    </div>            
                </div>
                <div class="divInformationRow">
                    <div class="divInformationCellLabel">
                        Label2
                    </div>
                    <div class="divCellControl">
                        Control2
                    </div>
                </div>
                <div class="divInformationRow">
                    <div class="divInformationCellLabel">
                        Label2
                    </div>
                    <div class="divCellControl">
                        Control2
                    </div>
                </div>                
            </div>    
            <div class="divInformationTable">
                <div class="divInformationRow">
                    <div class="divInformationHeaderCell">
                        Seller Information
                    </div>
                </div>    
                <div class="divInformationRow">
                    <div class="divInformationCellLabel">
                        Label3
                    </div>
                    <div class="divCellControl">
                    Control3
                    </div>            
                </div>
                <div class="divInformationRow">
                    <div class="divInformationCellLabel">
                        Label4
                    </div>
                    <div class="divCellControl">
                        Control4
                    </div>
                </div>
                <div class="divInformationRow">
                    <div class="divInformationCellLabel">
                        Label4
                    </div>
                    <div class="divCellControl">
                        Control4
                    </div>
                </div>                
            </div>
            <div class="divInformationTable">
                <div class="divInformationRow">
                    <div class="divInformationHeaderCell">
                        Seller Information
                    </div>
                </div>    
                <div class="divInformationRow">
                    <div class="divInformationCellLabel">
                        Label3
                    </div>
                    <div class="divCellControl">
                    Control3
                    </div>            
                </div>
                <div class="divInformationRow">
                    <div class="divInformationCellLabel">
                        Label4
                    </div>
                    <div class="divCellControl">
                        Control4
                    </div>
                </div>
                <div class="divInformationRow">
                    <div class="divInformationCellLabel">
                        Label4
                    </div>
                    <div class="divCellControl">
                        Control4
                    </div>
                </div>
            </div>
            <div class="divInformationTable">
                <div class="divInformationRow">
                    <div class="divInformationHeaderCell">
                        Seller Information
                    </div>
                </div>    
                <div class="divInformationRow">
                    <div class="divInformationCellLabel">
                        Label3
                    </div>
                    <div class="divCellControl">
                    Control3
                    </div>            
                </div>
                <div class="divInformationRow">
                    <div class="divInformationCellLabel">
                        Label4
                    </div>
                    <div class="divCellControl">
                        Control4
                    </div>
                </div>
                <div class="divInformationRow">
                    <div class="divInformationCellLabel">
                        Label4
                    </div>
                    <div class="divCellControl">
                        Control4
                    </div>
                </div>                
            </div>        
        </div>
        <div class="containerButtons">
            <div class="divButtonSection">
                OK
            </div>
            <div class="divButtonSection">
                Cancel
            </div>
        </div>
    </body>
    
</html>