How to check if a Document Library (SPDocumentLibrary) supports a particular ContentType - linq

I have a SharePoint 2010 site set up locally for debugging with the following topography:
Main (SPSite)
-> Toolbox (SPWeb)
-> MyTool (SPWeb)
I have created and deployed the following to Main:
Custom Field "RequestedBy"
Custom Field "OriginalRequestFileName"
Custom Content Type "RequestContentType" that contains the above two fields in addition to OOB fields
Custom List Definition "RequestListDefinition" based on the above ContentType
VisualWebPart "MyFileUploaderWebPart" that has a custom EditorPart to allow the user to define which document library the file should be uploaded to.
I have created an instance of a list "My Request List" in MyTool that's based on my custom list definition "RequestListDefinition".
In the EditorPart I've got a drop-down list of document libraries.
private void PopulateDocumentLibraryList(DropDownList dropDownList)
{
SPWeb currentWebsite = SPContext.Current.Web;
SPListCollection lists = currentWebsite.GetListsOfType(SPBaseType.DocumentLibrary);
if (lists.Count > 0)
{
List<SPDocumentLibrary> docLibraries = lists.Cast<SPList>()
.Select(list => list as SPDocumentLibrary)
.Where(library => library != null && !library.IsCatalog && !library.IsSiteAssetsLibrary)
.ToList();
dropDownList.DataSource = docLibraries;
dropDownList.DataTextField = "Title";
dropDownList.DataValueField = "ID";
dropDownList.DataBind();
// Default the selected item to the first entry
dropDownList.SelectedIndex = 0;
}
}
I would like to restrict the list of document libraries to only those that are derived from my custom list definition that I've deployed. I thought of doing this by checking the supported content types and thus tried altering the Where clause to:
private void PopulateDocumentLibraryList(DropDownList dropDownList)
{
SPWeb currentWebsite = SPContext.Current.Web;
SPListCollection lists = currentWebsite.GetListsOfType(SPBaseType.DocumentLibrary);
if (lists.Count > 0)
{
SPContentType voucherRequestListContentType = currentWebsite.ContentTypes["VoucherRequestContentType"];
List<SPDocumentLibrary> docLibraries = lists.Cast<SPList>()
.Select(list => list as SPDocumentLibrary)
.Where(library => library != null && !library.IsCatalog && !library.IsSiteAssetsLibrary && library.IsContentTypeAllowed(voucherRequestListContentType))
.ToList();
dropDownList.DataSource = docLibraries;
dropDownList.DataTextField = "Title";
dropDownList.DataValueField = "ID";
dropDownList.DataBind();
// Default the selected item to the first entry
dropDownList.SelectedIndex = 0;
}
}
It bombs out with the following error though:
Server Error in '/' Application.
--------------------------------------------------------------------------------
Value cannot be null.
Parameter name: ct
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: ct
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[ArgumentNullException: Value cannot be null.
Parameter name: ct]
Microsoft.SharePoint.SPList.IsContentTypeAllowed(SPContentType ct) +26981638
Dominos.OLO.WebParts.FileUploader.<>c__DisplayClass7.<PopulateDocumentLibraryList>b__4(SPDocumentLibrary library) +137
System.Linq.WhereEnumerableIterator`1.MoveNext() +269
System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +578
System.Linq.Enumerable.ToList(IEnumerable`1 source) +78
Dominos.OLO.WebParts.FileUploader.DocumentLibrarySelectorEditorPart.PopulateDocumentLibraryList(DropDownList dropDownList) +801
Dominos.OLO.WebParts.FileUploader.DocumentLibrarySelectorEditorPart.CreateChildControls() +154
System.Web.UI.Control.EnsureChildControls() +146
Dominos.OLO.WebParts.FileUploader.DocumentLibrarySelectorEditorPart.SyncChanges() +102
Microsoft.SharePoint.WebPartPages.ToolPane.OnSelectedWebPartChanged(Object sender, WebPartEventArgs e) +283
System.Web.UI.WebControls.WebParts.WebPartEventHandler.Invoke(Object sender, WebPartEventArgs e) +0
Microsoft.SharePoint.WebPartPages.SPWebPartManager.BeginWebPartEditing(WebPart webPart) +96
Microsoft.SharePoint.WebPartPages.SPWebPartManager.ShowToolPaneIfNecessary() +579
Microsoft.SharePoint.WebPartPages.SPWebPartManager.OnPageInitComplete(Object sender, EventArgs e) +296
System.EventHandler.Invoke(Object sender, EventArgs e) +0
System.Web.UI.Page.OnInitComplete(EventArgs e) +11056990
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1674
This suggests to me that it's failing to find the content type.
Another thought I had was to try and retrieve all lists that are of my custom list definition type "RequestListDefinition". However, SPWeb.GetListsOfType() takes an SPListTemplateType, which is an enum and thus doesn't contain my custom list definition. The documentation for SPListTemplateType (http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splisttemplatetype.aspx) suggests using a method that accepts a string or an int instead of SPListTemplateType but I haven't seen any documentation for this.
Can someone please help me work out either:
how I can get just those lists that are derived from my custom list definition; or
how I can get a hold of my custom content type; or
point me in the direction of a better solution for restricting the list of SPDocumentLibrary?
Thanks!!

Point 2:
The SPContentType should be retrieved via currentWebsite.AvailableContentTypes[name]. The ContentTypes property of a SPWeb does only return content types created on this particular web. However, AvailableContentTypes does return all content types available in the current site collection.
Update:
To check whether the list has your content type, you should use the content type collection on the list:
SPContentTypeId ctId = voucherRequestListContentType.Id;
// LINQ where clause:
.Where(library => (...) && library.ContentTypes[ctID] != null);
The method SPList.IsContentTypeAllowed checks if a given content type is supported on the list and not if the content type is part of the list. See the MSDN documentation SPList.IsContentTypeAllowed Method.

I found that IsApplicationList (SP 2013) was helpful in limiting to Document Libraries that were non-system libraries (i.e. IsApplicationList is true for _catalogs, SiteAssets, and SitePages, but not for Shared Documents).
In PowerShell, you can see this by running the following
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
Add-PsSnapin Microsoft.SharePoint.PowerShell
$site = Get-SPSite "http://sharePoint2013Url"
$webs = $site.AllWebs
foreach($web in $webs)
{
Write-Host "$($web.Url)"
foreach($list in $web.GetListsOfType([Microsoft.SharePoint.SPBaseType]::DocumentLibrary))
{
Write-Host "$($list.DefaultEditFormUrl) $($list.IsApplicationList)"
}
}

Related

MS Dynamics CE CRM 365 - Pre Operation Plugin - Given key was not present in the dictionary

After few years doing some other stuff I'm back to CRM business. I'm already questioning my life choices. I don't understand what is wrong here. I'm trying to create a simple plugin that will run on Incident / Case creation. It will look if description field contains a valid url and if yes then it should update first url that has been found to another field. Here's the plugin execution method.
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext pluginExecutionContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
if (pluginExecutionContext.InputParameters.Contains("Target") && pluginExecutionContext.InputParameters["Target"] is Entity)
{
Entity targetEntity = (Entity)pluginExecutionContext.InputParameters["Target"];
if (targetEntity.LogicalName != Incident.EntityLogicalName)
{
return;
}
IOrganizationServiceFactory orgServiceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService orgService = orgServiceFactory.CreateOrganizationService(pluginExecutionContext.UserId);
try
{
string desc = (string)targetEntity.Attributes["description"];
string pattern = #"\b(?:https?://|www\.)[^ \f\n\r\t\v\]]+\b";
MatchCollection collection = Regex.Matches(desc, pattern);
if (collection.Count > 0)
{
throw new Exception(collection[0].Value);
}
}
catch (Exception ex)
{
tracingService.Trace("Error in CaseUrlPlugin {0}", ex.ToString());
throw ex;
}
}
}
The problem is that when I'm creating a new case (description field filled with text and url) and hit save I get "Given key was not present in the dictionary" exception as if description field is not there. When I hit ok to that error window and hit save again then description field is founded and my code throws an exception with that link.
So why is not the description field present in the first time? I don't like the idea doing this post operation because that would require another sql transaction right (to save incident again)?
Aah ffs. The problem was that when I was trying to save for the first time I used CTRL + S while my focus was still on description field because that's the last field I was filling. Now it seems that UI doesn't register that field to be filled if focus on that field and hitting CTRL + S. Hitting save icon works because that would get focus off the description field. And of course in my case hitting ok for that error window also unfocus the field thus second save works.............
Well, at least I figured it out right after posting question here. Been trying to solve this way too long.

ASP.NET Resource handler mechanism in MVC4

Simple. I want to localize my application.
I've googled for days, and tried a million different approaches to reach my resources.
The only way I've been succesful is by using the standard asp.net folders "App_LocalResource", making the resource files public and giving them a Custom Tool Name. In the view I can then import the Custom Tool Name with #using.
My issue is that the language/resource items arent changing when I change the culture.
Here is how I change it in global.asax:
protected void Application_AcquireRequestState(object sender, EventArgs e)
{
if (HttpContext.Current.Session != null)
{
CultureInfo ci = (CultureInfo)this.Session["Culture"];
if (ci == null)
{
string langName = "en";
string autoLang = "";
if (HttpContext.Current.Request.UserLanguages != null && HttpContext.Current.Request.UserLanguages.Length != 0)
{
autoLang = HttpContext.Current.Request.UserLanguages[0].Substring(0, 2);
}
if (autoLang == "da")
langName = autoLang;
ci = new CultureInfo(langName);
this.Session["Culture"] = ci;
}
Thread.CurrentThread.CurrentUICulture = ci;
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);
}
}
So the culture is either da or en. But I noticed that the names of the resource files has to have a specific syntax. There has to be a default (in this case english) with no country/culture code and other that default has be named like reFile.da-DK.resx. It has to have both language and culture code.
I'm affraid the resource handler can recognize my file, because culture is set to "da" and not "da-DK". If I name my da resouce file to resFile.da.resx I cant import the Custom Tool Name which is my resouce files.
What do I do to solve this?
Use the full culture info string, ex:
var info = new CultureInfo("en-US")
Also for best practice move the code out into the Application_BeginRequest method, that's the standard location you'll see this type of code.

SelectedItem must always be set to a valid value. Windows Phone Local Database

I am using the local database example taht Microsoft created.
I can add items to the list, and delete them. But I now want to select the items and get the text of the item and use that in the next page.
This is the select changed event:
private void allToDoItemsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
NavigationService.Navigate(new Uri("/LiveTimes.xaml?selectedItem=" + allToDoItemsListBox.SelectedIndex, UriKind.Relative));
// string urlWIthData = string.Format("/LiveTimes.xaml?name={0}", " ");
// this.NavigationService.Navigate(new Uri(urlWIthData, UriKind.Relative));
}
Then this is the on page load on the other page.
string selectedIndex = "";
if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex))
{
int index = int.Parse(selectedIndex);
DataContext = App.ViewModel.HomeToDoItems[index];
}
Then when i use this, the error is on the DataContext line.
Whats the solution?
There is no problem in the above code that you have shown, The actual problem may be in the way how you defined your ViewModel and HomeToDoItems . It helps us to solve your problem, if you can show some of that code.
Also before setting your data to DataContext, try the following steps:
First, make sure you are getting the valid selectedIndex.
var tempData = App.ViewModel.HomeToDoItems[index];
DataContext = tempData;
And then insert a break point at tempData to check whether you are getting the expected data.
This answer may not solve your problem, but guide you in identifying the actual problem.

How to pull the finalUri property from an async image lookup result?

In windows phone 7 I'm doing a simple async lookup to find an image by uri and set the returned binary as the source for an image control.
public object SetImageFromUri(string uri)
{
var wc = new WebClient();
wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
wc.OpenReadAsync(new Uri(uri), wc);
return null;
}
void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null && !e.Cancelled)
{
var image = new BitmapImage();
image.SetSource(e.Result);
//e.Result has a property in the memory stream labeled finalUri
//someImageControl.Source = image;
}
}
My question is- how can I pull out the final uri property from the e.Result so I can see what image control it's associated with
Thank you in advance
Instead of passing the WebClient through as the second parameter, pass the Uri (or some other piece of usefule state information)
wc.OpenReadAsync(new Uri(uri), uri);
You can then access this in your callback
var uri = (string)e.UserState;
Due to specific restrictions implemented in the Reflection mechanism, you cannot access internal content from sandboxed code. Ultimately, you would want to use something like this:
FieldInfo f = e.Result.GetType().GetField("_finalUri", BindingFlags.NonPublic | BindingFlags.DeclaredOnly | BindingFlags.Instance);
Uri n = (Uri)f.GetValue(e.Result);
However, this will cause a FieldAccessException. If you are not using a redirect URI, then you can simply reuse the parameter that is initially passed to your method. If not, you need to check HttpWebRequest and follow the idea I outlined a couple of days ago.
You could also just bind directly to the Image, and use the LowProfileImageLoader, to avoid it blocking the UI thread during the load. (Remember to set a FallBack image)

LINQ CRM 2011 Update - Create

I notice the the CRM moderator David Jennaway on the technet forum states that you can't use LINQ to update/Create records in CRM 2011 see here http://social.microsoft.com/Forums/en-IE/crmdevelopment/thread/682a7be2-1c07-497e-8f58-cea55c298062
But I have seen a few threads that make it seem as if it should work. Here is my attempt which doesn't work. Any ideas why not?
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
OrganizationServiceContext orgContext = new OrganizationServiceContext(service);
EntityState state = new EntityState();
state = EntityState.Changed;
var counter = from c in orgContext.CreateQuery<pcx_entitycounter>()
where c.pcx_name.Contains("pcx_candidate")
select new pcx_entitycounter
{Id = c.Id,
pcx_name = c.pcx_name, pcx_Sequence = c.pcx_Sequence, pcx_Prefix = c.pcx_Prefix
};
foreach (var c in counter)
{
string prefix = c.pcx_Prefix.ToString(); ;
string sequence = c.pcx_Sequence.ToString();
c.pcx_Sequence = c.pcx_Sequence + 1;
c.EntityState = state;
**service.Update(c);** //FAILS HERE
}
In my experience, it's been difficult-to-impossible to retrieve an entity from the Context, update it, then use the Service to save the changes. It has caused me headaches figuring it out!
Since your retrieval code uses a query from the Context, all of those entities should be attached to the Context and their states are being tracked. Thus you need to use the Context's method for updating:
foreach (var c in counter) {
string prefix = c.pcx_Prefix.ToString(); ;
string sequence = c.pcx_Sequence.ToString();
c.pcx_Sequence = c.pcx_Sequence + 1;
// Use the Context to save changes
orgContext.UpdateObject(c);
orgContext.SaveChanges();
}
Since a lot of my code will retrieve entities in different ways (i.e. Service or Context) depending on the situation, I have developed a simple method that knows how to update the entity correctly. To expand on your example, you might have an update method that looks like:
public void UpdatePcxEntityCounter(pcx_entitycounter c) {
if (!orgContext.IsAttached(c)) {
service.Update(c);
}
else {
orgContext.UpdateObject(c);
orgContext.SaveChanges();
}
}
This assumes both orgContext and service are available at a scope above that of the method. Otherwise, they'd have to be passed as additional parameters.
Without seeing the difficult its difficult to discern what the issue is but have you tried using orgContext.UpdateObject(c); before doing the update step? Also, not sure why you are assigning the prefix and sequence to local variables within your loop since they don't appear to be being used. Its possible that you are getting a SOAP Exception or something for assigning values that don't work. Do you have any plugins registered on the entity?
See the following links for possible resolutions -
How to update a CRM 2011 Entity using LINQ in a Plugin?
http://social.microsoft.com/Forums/en-US/crmdevelopment/thread/7ae89b3b-6eca-4876-9513-042739fa432a

Resources