Format Date and Time

Format date and time :

m_CalculatedOutOfOfficeStartDate = outOfOfficeStartDate.ToString("dd/MM/yyyy");
m_CalculatedOutOfOfficeEndDate = outOfOfficeEndDate.ToString("dd/MM/yyyy");

Hope this helps-
Rehman.

Ensure trailing slash

Sometimes you don’t know if you’ll get a trailing slash or not with a web.Url or site.Url. Use the method:

public static string EnsureTrailingSlash(string strUrl)
        {
            if (!strUrl.EndsWith("/", StringComparison.OrdinalIgnoreCase))
            {
                strUrl += "/";
            }
            return strUrl;
        }

Call like this:
SharedUrlUtility.EnsureTrailingSlash(SharedUserProfiles.MySiteHostUrl);

Hope this helps-
Rehman.

Create, Add, Delete content type (attach a custom document template)

July 9, 2012 1 comment

Creates a custom content type…attaches a custom document template with it…enables content types on document library…..deletes “Document” content type from the library….attaches our custom content type to it…

private void ProcessMyCVContentType(SPWeb web)
        {
            try
            {
                web.AllowUnsafeUpdates = true;
                SPDocumentLibrary cvDocLib = web.Lists.TryGetList(Config.ListName_MyCVs) as SPDocumentLibrary;
                SPContentTypeId myContentTypeId = new SPContentTypeId("0x010100FA0963FA69A648AA916D2E41284FC3D5");
                SPContentType myContentType = web.ContentTypes[myContentTypeId];
                if (myContentType == null)
                {
                    myContentType = new SPContentType(myContentTypeId, web.ContentTypes, "MyCVs");
                    web.ContentTypes.Add(myContentType);
                    myContentType.Group = "Custom Content Types";
                    myContentType.DocumentTemplate = "/my/_cts/myconenttypetemplate/template.dotx"; //SharedUrlUtility.EnsureTrailingSlash(web.ServerRelativeUrl) + Config.CVTemplatePath; //must be a relative url, no http:// etc in it.....otherwise u get "value does not fall within the expected range"......... 
SPFieldLink fieldLink = new SPFieldLink(cvDocLib.Fields["Primary"]);
                    myContentType.FieldLinks.Add(fieldLink);
                    fieldLink = new SPFieldLink(cvDocLib.Fields["Description"]);
                    myContentType.FieldLinks.Add(fieldLink);
                    myContentType.Update();
                }                            
                                            
                if (myContentType != null && cvDocLib != null)
                {
                    cvDocLib.ContentTypesEnabled = true;
                    if (cvDocLib.ContentTypes["Document"] != null)
                        cvDocLib.ContentTypes.Delete(cvDocLib.ContentTypes["Document"].Id);
                    cvDocLib.ContentTypes.Add(myContentType);
                    cvDocLib.Update();
                }                       
            }
            catch (Exception ex)
            {
                Log.LogError("MyProjName", ex);
            }
        }

make sure to put your custom template (e.g. customtemplate.dotx) at the specified location or a location of your choice (usually _cts folder is used for content types, see in sharepoint designer) and mention a relative url to it…….

Hope this helps-
Rehman.

Enable Full Toolbar on an XSLT List View Web Part (XsltListViewWebPart) / on a list

July 9, 2012 3 comments

Enabling toolbar for a list programmatically means to enable it on xslt list view web part..Here’s the code:

private void EnableToolbarOnXsltListViewWP(string viewName, string listName, string webUrl)
        {
            using (var spSite = new SPSite(webUrl))
            {
                using (SPWeb web = spSite.OpenWeb())
                {
                    spSite.AllowUnsafeUpdates = true;
                    web.AllowUnsafeUpdates = true;
                    SPList spList = web.Lists[listName];
                    SPView spView = spList.Views[viewName];
                    SPLimitedWebPartManager spLimitedWebPartManager = web.GetLimitedWebPartManager(spView.Url, PersonalizationScope.Shared);
                    foreach (System.Web.UI.WebControls.WebParts.WebPart webPart in spLimitedWebPartManager.WebParts)
                    {
                        if (webPart.GetType().Name.Equals("XsltListViewWebPart") && webPart.Title == Config.ListName_MyCVs)
                        {
                            try
                            {
                                MethodInfo ensureViewMethod = webPart.GetType().GetMethod("EnsureView", BindingFlags.Instance | BindingFlags.NonPublic);
                                object[] ensureViewParams = { };
                                ensureViewMethod.Invoke(webPart, ensureViewParams);
                                FieldInfo viewFieldInfo = webPart.GetType().GetField("view", BindingFlags.NonPublic | BindingFlags.Instance);
                                var view = viewFieldInfo.GetValue(webPart) as SPView;
                                Type[] toolbarMethodParamTypes = { Type.GetType("System.String") };
                                MethodInfo setToolbarTypeMethod = view.GetType().GetMethod("SetToolbarType", BindingFlags.Instance | BindingFlags.NonPublic, null, toolbarMethodParamTypes, null);
                                object[] setToolbarParam = { "ShowToolbar" };
                                setToolbarTypeMethod.Invoke(view, setToolbarParam);
                                view.Update();
                            }
                            catch (Exception ex)
                            {
                                Log.LogError("MyProjName", ex);
                            }
                        }
                    }
                    spSite.AllowUnsafeUpdates = false;
                }
            }
        }

Hope this helps-
Rehman.

Hide Site Actions


<!--Hide Site Actions from readers/visitors -->

<Sharepoint:SPSecurityTrimmedControl runat="server" Permissions="EditListItems">

<!-- Site Actions content here... -->

</Sharepoint:SPSecurityTrimmedControl>

Hope this helps-
Rehman.

Visual Studio 2012 Benefits

Create Lists and Content Types by Using New Designers
Create Site Columns
Create Silverlight Web Parts
Publish SharePoint Solutions to Remote SharePoint Servers
Test SharePoint Performance by Using Profiling Tools
Create Sandboxed Visual Web Parts
Improved Support for Sandboxed Solutions.
Support for JavaScript Debugging and IntelliSense for JavaScript
Streamlined SharePoint Project Templates
Test Your Code by Using Microsoft Fakes Framework

http://msdn.microsoft.com/en-us/library/ee290856%28v=vs.110%29.aspx

Update a list view in sharepoint

Update a list view:

public static void UpdateListView(SPWeb web, string listName, string viewName, string fields, string query, int rowLimit, bool isDefault, string scope, bool isTabularView, bool allowInlineEditing)
        {
            try
            {
                web.AllowUnsafeUpdates = true;
                SPList list = web.Lists.TryGetList(listName);

                if (list != null)
                {
                    SPView view = list.Views[viewName];

                    if (view != null)
                    {
                        SPViewFieldCollection fieldColl = view.ViewFields;

                        string[] flds = fields.Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                        fieldColl.DeleteAll();

                        foreach (string fld in flds)
                        {
                            fieldColl.Add(fld.Trim());
                        }

                        if (!string.IsNullOrEmpty(scope))
                        {
                            SPViewScope type = SPViewScope.Default;
                            foreach (var value in Enum.GetValues(typeof(SPViewScope)))
                            {
                                if (value.ToString() == scope)
                                {
                                    type = (SPViewScope)value;
                                    break;
                                }
                            }
                            view.Scope = type;
                        }

                        view.DefaultView = isDefault;
                        view.Query = query;
                        view.RowLimit = Convert.ToUInt32(rowLimit);
                        view.TabularView = isTabularView;
                        view.InlineEdit = allowInlineEditing.ToString();
                        view.Update();
                    }
                }
            }
            catch (Exception err)
            {
                Log.LogError(err.Message, err);
            }
            finally
            {
                web.AllowUnsafeUpdates = false;
            }
        }

Call like this:

SPHelper.UpdateListView(myweb, “Your List Name”, “All Pictures”, “Title,Name,Description,Picture Size,Type”, string.Empty, 100, true, string.Empty, false, false);

“query” if you wish to filter items……
where viewName is an existing view……..

Hope this helps–
Rehman.

Follow

Get every new post delivered to your Inbox.

Join 37 other followers