Wednesday, July 25, 2012

Set field to read only on EditForm


Solution: Open Sharepoint Designer, select “Lists and Libraries” then select the list, then select “EditForm.aspx” from “Forms” panel.

Select “Advanced Mode.”

Click “Preview in Browser” and set the value in the field to something distinctive (e.g. “Wokka Wokka”)

From the browser, select “View Source” and search for the distinctive text.

You will see something like this:

<input name="ctl00$TextField" type="text" value="wokka wokka" maxlength="255" id="ctl00_ctl00_ctl00_TextField" title="Title" class="ms-long ms-spellcheck-true" /><br />

Copy the value in “id” into the text below, then go back to Sharepoint Designer and append that text at the bottom of the EditForm.aspx file.

Click Preview In Browser again and the field should be read only.

<asp:Content ID="Script" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
<script type="text/javascript">
function setControlReadOnly()
{
var elements=document.getElementById('ctl00_m_g_19d74208_fb82_4a74_a3b0_0c5d16ec6591_ctl00_ctl05_ctl00_ctl00_ctl00_ctl04_ctl00_ctl00_TextField');
if (elements == null)
{
alert("Could not set control to read only.");
}
elements.readOnly=true ;
}
_spBodyOnLoadFunctionNames.push("setControlReadOnly")
</script>
</asp:Content>

Tuesday, July 24, 2012

query.Query = "<Where><Gt><FieldRef Name='ID'/><Value Type='Counter'>0</Value></Gt></Where><OrderBy><FieldRef Name='Titl'/></OrderBy>";

Create a NetworkCredential from a SPManagedAccount

        private static NetworkCredential GetManagedAccountCredential(string managedAccount)
        {
            NetworkCredential result = null;
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPFarmManagedAccountCollection accounts = new SPFarmManagedAccountCollection(SPFarm.Local);
                    SPManagedAccount account = accounts.FindOrCreateAccount(managedAccount);
                    string[] usernameSplit = account.Username.Split('\\');
                    string username = usernameSplit[1];
                    string domain = usernameSplit[0];
                    var securePassword = (SPEncryptedString)account
                                                            .GetType()
                                                            .GetField("m_Password",
                                                              System.Reflection.BindingFlags.GetField |
                                                              System.Reflection.BindingFlags.Instance |
                                                              System.Reflection.BindingFlags.NonPublic)
                                                            .GetValue(account);
                   // TODO - remove these 3 lines when .Net 4.0 is available and we can create NetworkCredential with SecureString
                    var intptr = System.IntPtr.Zero;
                    var unmanagedString = Marshal.SecureStringToGlobalAllocUnicode(securePassword.SecureStringValue);
                    var password = Marshal.PtrToStringUni(unmanagedString);
                    Marshal.ZeroFreeGlobalAllocUnicode(unmanagedString);
                    result = new NetworkCredential(username, password, domain);
                    password = null;
                });
            }
            catch (Exception ex)
            {
                Logging.LogException("GetManagedAccountCredential", ex);
            }
            return result;
        }

Wednesday, July 18, 2012

Get Sharepoint data from remote sharepoint site

        private static ListItemCollection GetSharepointData(string siteUrl, string sharepointList, string sharepointView)
        {
            ListItemCollection listItems = null;
            try
            {
                Console.WriteLine("Connection to Sharepoint: '{0}'", siteUrl);
                ClientContext clientContext = new ClientContext(siteUrl);
                Console.WriteLine("Opening Sharepoint list: '{0}'", sharepointList);
                List list = clientContext.Web.Lists.GetByTitle(sharepointList);
                FieldCollection fields = list.Fields;
                clientContext.Load(list);
                clientContext.Load(fields, items => items.Include
                                            (item => item.FieldTypeKind,
                                             item => item.Title,
                                             item => item.InternalName));
                clientContext.ExecuteQuery();
                CamlQuery camlQuery = new CamlQuery();
                listItems = list.GetItems(camlQuery);
                clientContext.Load(listItems);
                clientContext.ExecuteQuery();
            }
            catch (Exception ex)
            {
                Console.WriteLine("EXCEPTION: GetSharepointData Failed to load list {0}", ex.Message);
            }
            return listItems;
        }

Thursday, July 12, 2012

View/Edit item in popup window link

string viewUrl = list.DefaultViewUrl;
// If current user can edit the list, then we set viewUrl to the edit form
if ((list.EffectiveBasePermissions & SPBasePermissions.EditListItems) == SPBasePermissions.EditListItems)
{
    viewUrl = list.DefaultEditFormUrl;
}
string editLink = @"<a href=""javascript:void(0)"" onclick=""javascript:SP.UI.ModalDialog.showModalDialog({{ url: '{0}?ID={1}', title: '{2}' }}); return false;"">{2}</A>";
string linkUrl = string.Format(editLink, viewUrl, item.ID, item.title);