Showing posts with label aspx. Show all posts
Showing posts with label aspx. Show all posts

August 07, 2014

How to Covert string to valid file name format, get valid file name, file name validation

This is general question that How to Covert string to a file name to save on drive or some location. our Keyboard have so many special characters and looking for more symbols in our keyboard,
That's good for users but for developers its headache coz out of those characters few are allowed in file name,
That makes issue sometime while reading/writing or creating file.

if user entered some incorrect word so instead of giving error to end user we can replace all those special characters by any specific character like I have done with char '_' underscore,
The file name is very important in terms of search or crawl files/file name.


So to make it simple, easy and working I have would prefer to use below way to do so.....


C# Code:
private string ConverToStandardFileName(string accountName)
        {
            foreach (char c in System.IO.Path.GetInvalidFileNameChars())
            {
                accountName = accountName.Replace(c, '_');
            }
            return accountName;
        }


That can be used in any aspx or C#/Windows appliaction. Other ways are also suggested over internet like .Replace("%","").Replace("&",""),Replace("*","") etc....


Other ways/suggestion are welcome, also shared you findings on this.


--

Regards,
Praveen Pandit
Keep Sharing Knowledge!!

May 04, 2014

SharePoint 2013 auto fill people picker, auto fill people editor in application page (aspx) C#

Microsoft always comes with some new and more user friendly features in every new version. Well this client people picker (which is auto fill by user name, email or login id) new and simple control introduced in SharePoint 2013, that will save lot of time of ours.

instead of using below people picker of sharepoint 2010

<SharePoint:PeopleEditor ID="spPeoplePicker" runat="server" Width="350" SelectionSet="User" />

and setting javascript we can use  PeopleEditor to make it easy and faster for end user we can use below control in SP2013

aspx syntax :
<SharePoint:ClientPeoplePicker ValidationEnabled="true" ID="pplDemo" runat="server" VisibleSuggestions="5" AutoFillEnabled="True" AllowEmailAddresses="true" Rows="1" AllowMultipleEntities="false" CssClass="ms-long ms-spellcheck-true" Height="85px"  />



C# Code to get SPUser:
if( pplDemo.ResolvedEntities.Count > 0)
{
SPUser oUser = oWeb.SiteUsers[((PickerEntity)pplDemo.ResolvedEntities[0]).Key];

                         if (oUser != null)
                         {
                              // do your stuff.
                         }
}

C# Code to Set User to Client People Picker control:
 
 pplDemo.InitialUserAccounts = "UserEmail1";
 



Thanks,
remaining getting and saving values in SharePoint is same as earlier.

You can Share your findings or issue here.


--
Thanks,
Praveen Pandit

August 08, 2013

SharePoint Get all permission and list of site of SPuser / current user C# programatically

Well I have achieved this by using SPRoles, but as all knows SPRoles has been deprecated from September 2012, giving warning user SPRoleAssignment and SPRoleDefinition class.



Its a bit confusing so lets understand about SPRoleDefinition and SPRoleAssignment.

SPRoleDefinition represents the SharePoint OOB permission levels like "Full Control", "Contribute", "Read"while a Role Assignment contains a collection of Role Definitions ItemAdd, ItemView, UpdateItem, EditPage etc....

Here is a simple code snippet to get current user's / give user's permissions



Code:
string permission = string.Empty;

            using (SPSite oSite = new SPSite(SPContext.Current.Site.Url))
            {
                foreach (SPWeb oWeb in oSite.RootWeb.GetSubwebsForCurrentUser())
                {
                    SPPermissionInfo info = oWeb.GetUserEffectivePermissionInfo(oUser.LoginName);

                    permission = string.Empty;
                    foreach (SPRoleAssignment roleA in info.RoleAssignments)
                    {
                        foreach (SPRoleDefinition roledef in roleA.RoleDefinitionBindings)
                        {
                            permission += roledef.Name.ToString() + ", ";
                        }
                    }
                    permission = " [" + permission.TrimEnd(", ".ToCharArray()) + "]";
           }
     }

It will return users permission from all sites on which user has permission in format of [Read, Contribute]  /  [Read]  /  [Full Control]

oUser is SPUser object that has user details you can pass Currentuser also by using below lines:
SPUser ouser = SPContext.Current.Web.CurrentUser;




Please share any issue or findings on this.



--
Regards,
Praveen Pandit